List Comprehensions


Another way to work with lists is called “List Comprehensions”, this is quite immediate, we have a list

List = [1,2,3].

and we want to iterate over every item in the list … we can do:

[Item * 2 || Item <- List].

This way we are asking a list as result of execution (using []). We are asking that every Item in List (the code after ||) must be used in the code before || (Item * 2).

So we asked a thing like:

lists:map(fun(Item) -> 2*Item end, List).

without having to define a fun! useful 🙂



Funs are called ‘anonymous functions’, let’s try to define one:

Sum = fun(X, Y) -> X+Y end.

This funs simply sums X and Y.

In erlang there are (and is possible to define) functions that accept a fun as parameter, this kind of functions are called “higher-order functions”.

Let’s define a list with our wishes:

ThingsIDesire = [iphone, mouse, keyboard].

Now we can define a fun that remove iphone from the list (we have already a phone and we can’t spend more for this kind of things!):

GoAwayIphone = fun(Devices) -> lists:delete(iphone, Devices) end.

Note: doing lists:delete we are using a function available in erlang’s standard lib, you can see more of it -> http://erlang.org/doc/apps/stdlib/index.html

Now if we execute:


we will obtain:


To use a fun inside another function is really simple, in the standard lib there is a function called lists:member(item, list) that returns true if <item> is in the <list>.
If we try:

lists:member(iphone, ThingsIDesire).

it returns true (because iphone is in ThingsIDesire), but if I pass GoAwayIphone(ThingsIDesire) as second argument like:

lists:member(iphone, GoAwayIphone(ThingsIDesire)).

I got false!

Is also possible for a function to return a fun, for example we can do:

DevicesToBlock = [iphone].
CheckPresenceOfBlockedDevices = fun(Devices) -> (fun(Device) -> lists:member(Device, Devices) end) end.

We defined fun(Devices) that returns fun(Device), now let’s define the function that actually check an item:

CheckIfThisIsABlockedDevice = CheckPresenceOfBlockedDevices(DevicesToBlock).


CheckIfThisIsABlockedDevice(iphone). returns true and
CheckIfThisIsABlockedDevice(mouse). returns false

The argument passed to CheckIfThisIsABlockedDevice is passed to fun(Device) that checks if the passed item is in the DevicesToBlock list.

By now it sounds quite confusing … but I can imagine some benefit of it!



All the functions will be stored in modules, and they must be compiled before the code can be run.
When you write a module its extension will be .erl but when it will be compiled it changes in .beam

ThingsToBuy = [ {iphone, 100},
{mac, 1500},
{mouse, 20},
{keyboard, 30}].

This is the list of items we want to buy, how much money will we spend to buy all these items? Let’s create a module to calculate it!


total([{_, Money}|T]) -> Money + total(T);
total([]) -> 0.

(save this code in cost.erl)

Now in shell you have to compile your module writing:


Now you can calculate the sum of all these items doing:




When you are working with erlang shell you will use much variables and sometimes the fact you can’t change a value force you to choose funny names.

There is a way to flush all variables in erlang shell, this way every variable will be unbound and you can re-utilize its name 🙂

Simply do f(). in the shell and all variables will be flushed 🙂