I have recently started working in Erlang and there are many things to like about the language, the data isolation, the functional paradigm and the raw speed. However, one thing that no one loves about erlang is its syntax. Even after a weeks of working on the language you will still be searching through the manual to decipher what the hell does this symbol do. Since its a symbol its really hard to google the right answer unless you already know what you are looking for.
List comprehensions
List comprehensions are a concise way to create and modify lists in erlang using the mathematical concept of sets. Sets is math are often described using their properties and constrains in relation to other sets. For example you might describe a set as "All values of x squared such that x is in set L". Or more concisely {x*x : x in L}. The erlang code for this will be
We can even put constraints on the values of the original set that will be used in the new set construction. For example lets say our definition now is "All values of x squared such that x is an even number in the set L". The erlang code for this is:
Lastly we can even use multiple sets as inputs to a list comprehension call. For example a list defines as "All values of x multiplied by y such that x is an even number in the set L and y is an odd number is set S". The code for this will be as follows:
External links
Key words for the weary googler
So in the interests of googlers here are some key words that you might search for.
-
What does “ ” do in erlang. -
What does “[ ]” do in erlang. - Erlang Double Pipe