How to overcome a mental block when learning Erlang?

Here is just a small sample of what I am trying to digest. A simple Tuple:
‘’’
tuple_size({abc, {def, 123}, ghi}).
‘’’
It has 3 elements - no problem. What I am trying to grasp is why the ( ). Lists are in […].
I tried without the ( ) and got a syntax error. This is what I am grappling with. The theory etc is ok, I am currently using the book Programming Erlang 2nd edition, but in the code explanation, there is no explanation why the use of ( ).

2 Likes

Not difficult to understand. It’s not a ‘special use of parentheses’. It’s just a function call. Functions require parentheses. They are not optional like in Elixir.

tuple_size/1 is a function (in the erlang module, erlang:tuple_size/1)

2 Likes

Forgot to mention in my previous reply.

When we bring on programmers new to Erlang in my teams we generally recommend they start out with:

  1. Learn You Some Erlang
  2. Erlang track on Exercism

We’ve found that these have been a great help in getting them up to speed quickly.

5 Likes

To break that one down into maybe too much detail:
The syntax to call a function is similar to many other languages, function_name(Argument1, Argument2, ...), so the function you are calling is called tuple_size/1. The /1 part signifies the arity, in other words the amount of arguments the function expects.

The argument itself is a tuple, syntax {Element1, Element2, ...}, a collection of an arbitrary but fixed number of elements, in your case, three - an atom, a tuple, and another atom.

3 Likes

That you so much. So ALL functions require parentheses. Much appreciated.

2 Likes

Thanks. I also have that book Learn you some Erlang, however, I am finding that a bit “quick”. Once I get more experience, I will work through that as well. The Erlang track on Exercism, I will try again. For the past few weeks, the Wifi has been more down than up.

Much appreciated.

2 Likes

Hm , you think so? IMO this is the easiest resource to get going with Erlang, and it can carry you a long way.
It is “quick” in the sense that it doesn’t dwell on any subject in too great (but enough) depth, so you get a fair sense of progress instead of getting overloaded with too many confusing details which you can find out later from the more in-depth resources.
So maybe it’s you who are moving through the book too fast, not giving yourself enough time to play and experiment with the examples to really understand what is going on and what they are about?

In that sense, I fully agree with what @Maria-12648430 said earlier in this thread:

3 Likes

I tend to move fast and when I get stuck, I “freeze”. Here is an example of me Freezing and not being able to figure out whatI did wrong. Here is an example in the book:

“”"
1> Module = examples.
examples
2> Function = even.
even
3> Arguments = [10].
[10]
4> apply(Module, Function, Arguments).
true
“”"

When I type it into the ‘erl’
I get the following:

Module = examples.
examples
2> Function = even.
even
3> Arguments = [10].
“\n”

4> apply(Module, Function, Arguments).
I get the following error:** exception error: undefined function examples:even/1

I have been trying for over an hour to try to fix this as I do not want to progress before I can sort this out.

2 Likes

There either is no module examples, or it doesn’t have an (exported) even/1 function. apply(Module, Function, Args) in this case is apply(example, even, [10]), and that boils down to the same as examples:even(10).

Try this instead (from the Erlang docs on erlang:apply/3):

> apply(lists, reverse, [[a, b, c]]).
[c,b,a]

This should work, as the lists module is loaded (as part of Erlangs stdlib) and it has an exported reverse/1 function, this boils down to lists:reverse([a, b, c]) giving you the reversed list, ie [c, b, a].

This is confusing, yes :wink: There are no strings in Erlang, what you’d call a string is represented as a list of ASCII codes. When the shell encounters a list containing integers that correspond to printable ASCII characters, it prints them string-like. In this case, 10 is the ASCII code for newline, and that is printed as \n.

2 Likes

What book, btw, and where?

2 Likes

Erlang Programming_ A Concurrent Approach to Software Development

Page 56.

2 Likes

Ah, ok, the passage indeed omits that part :sweat_smile: So yes, there needs to be an examples module with an even/1 function in it loaded for this to work.

The examples module itself is described on page 52, so I assume you already have examples.erl somewhere, you just need to compile and/or load it. Go to the directory where you put that file, open an Erlang shell (type erl on Linux, werl on Windows), then use c(examples). to compile and load it (the call should return {ok, examples}). Now apply(examples, even, [10]) (page 56) should work, as well as examples:even(10).

2 Likes

Thank you. I just typed the example in thd shell.

2 Likes

I think you having problems here illustrates a point I (and @juhlig) tried to make earlier: Concentrate on getting the easy stuff working, don’t cram tons of tiny details in your brain.

This book (Erlang Programming) is not very suitable for a beginner IMO. It goes into much detail on every point and loads your brain with things that, while doubtless useful to know at some point, are not needed when you’re trying to get to grips with the language.

Like Meta Programming. You’ll certainly not be using that for a while, if ever, not for the rest of your journey to become an Erlang programmer in the first place anyway. In years of Erlang, I only had occasion to use apply a few times. Nevertheless, it’ll take up space in your brain, and time (and frustration) to work through and understand that example, without much reward.
Or, from a different perspective: What use is the ability to meta-program a language that you don’t (yet) understand when used directly, ie without the meta part?

That said, I’d really recommend “Learn You Some Erlang” instead. It may be “quick” (as you call it) in that it gloats over details, but it is much more beginner-friendly, granting you quick rewards. It takes you by the hand, figuratively speaking. All the examples work out of the box (that is, they did when the book was written, I don’t know if it was updated to keep track with changes in OTP since). Even if they don’t work at first try, it’s usually easy to figure out what you did wrong by just going back a page or two. And even if you don’t get a specific example to work, you can usually just keep on reading and still understand what is going on.

5 Likes

I created a module:

‘’’
-module(exam).
-export([even/1]).

even(Arguments) →
Arguments = [10].
‘’’

It compiles ok. When I do a function call:

exam:even(10).
** exception error: no match of right hand side value “\n”
in function exam:even/1 (exam.erl, line 5)

Do I need to import something into the module?

2 Likes

Uh, no :sweat_smile:

This is wrong:

even(Arguments) ->
Arguments = [10].

and wouldn’t even do what the function name suggests (testing for even-ness) if it worked.

Instead of presenting you with the correct solution, however, let me point you to this, this and this chapter in Learn You Some Erlang and see if you can’t figure it out by yourself :wink: I think by doing it this way, you will get to grips with the respective concepts better than if I just presented you with a solution, and maybe you will see the value of this book for your learning experience :wink:

5 Likes

@juhlig we should charge @MononcQc a commission for promoting his book :grin:

5 Likes

I do not want to start any wars here regarding learning material. As a 55 year old who has never programmed in his life, I saw Erlang and for some reason known only to who knows, I fell in love with it - even before trying any code. (I hope that makes sense).

Now I have these 2 books:
Erlang Programming_ A Concurrent Approach to Software Development (which I am currently working through)

Learn You Some Erlang for Great Good. As mentioned, this was a bit too “quick”, however, I am willing to work through it as well.

What I need (and only beginning to understand) is the terms used as well as the flow of the programs in functional programming (I know Erlang is not FULLY functional, but no partner is perfect :slight_smile: ).

Do I continue with my current book or switch over. I found that reading different books confuses me more than ever. I am very very old school.

2 Likes

@Maria-12648430 was just joking (or so I think, you never know with her :grin:), and as for myself, I’m not saying Francescos book is bad :wink: It’s really great, but from what I see I’d say it isn’t the best for you at this point.

Look at it this way: You’re currently trying to understand meta programming (apply etc), something that is both quite advanced and that you will probably never need in the foreseeable future. At the same time, you are struggling (or maybe better, are not comfortable with) the matching operator (=) which is an absolute basic building block, something that you will absolutely need countless times of every day for the rest of your Erlang life :sweat_smile:

(No offense btw, I just have a feeling that your current approach will ultimately lead only to frustration, and even if it doesn’t, you could have progressed much faster and easier :blush:)

Yes :wink:

4 Likes

Thank you. In your opinion (and I accept all opinions here as you are all far far ahead of me in Erlang and programming), what book would be a great starter. I like watching videos and doing online tutorials, however, our internet is not reliable as we have constant power failures here that last hours.

2 Likes