small medium large xlarge

12 Dec 2011, 20:49
Marco (3 posts)


i’ve read the book for a whole night, typing the example codes, trying hard to follow and understand what i’m doing. It’s very hard to follow for me, because of the whole recursiveness. Maybe programming for years in “c-syntax-languages” makes learning erlang so hard for me (or is it not just me who thinks it’s really not easy getting used to erlang?).

So far, so good. After thinking about the examples for a while i understood them. But then came quicksort. From nothing there’s now list comprehension with comata in the generator. What does that mean? How do i read sort([X || X < - T, X < Pivot])? What exactly does “++ [Pivot] ++” mean? The little sentence that ++ is the infix append operator didn’t help me out well. What happens here?

I hope someone could help me with understanding this, because i feel really stupid at the moment. I want to understand erlang because of the cool things you can do with it, but i’m kind of frustrated in such an early part of learning the language.

05 Jan 2012, 20:29
Stuart Coyle (1 post)

The code sort([X || X < – T, X < Pivot]) can be read as: sort X for all X where X is an element of T and X is less than Pivot. List comprehensions are expressive and terse. It would be a much longer bit of code in C.

An infix operator is one which sits between it’s arguments like the + in A + B. The ++ operator concatenates lists. A ++ B is the same as calling list:append(A,B). Play with it in the erl console to see how it works.

06 Jan 2012, 19:29
Marco (3 posts)

Thank you so much. Now that you said it it’s kind of obvious to me how the list comprehension has to be read. I think my background with C style languages sometimes stands in my way of getting concepts of different syntax style languages. Can’t see the forest because of all the trees ;)

24 Jun 2012, 02:02
Shailen Karur (3 posts)

One simple way of thinking about list comprehensions is to think of them as you would when you work with sets, using statements like {X | X^2 + 2*X - 1 < 23} to define a set of values that correspond to the limitation on the right of the ‘|’, the ‘such that’.

No doubt, this is a simple example, but it could be a useful way to build up from.

HTH, BR, Shailen

  You must be logged in to comment