Higher-order functions
lists:map
The map function applies a fun to every item and returns the list of results. The input list is untouched.
This module wraps map, filter, foldl and foreach in named functions; save it as higher.erl.
-module(higher).
-export([squares/1, evens/1, total/1, shout/1]).
squares(List) ->
lists:map(fun(X) -> X * X end, List).
evens(List) ->
lists:filter(fun is_even/1, List).
total(List) ->
lists:foldl(fun(X, Acc) -> Acc + X end, 0, List).
shout(List) ->
lists:foreach(fun(X) -> io:format("~s!~n", [X]) end, List).
is_even(X) ->
X rem 2 =:= 0.
erlc higher.erl
erl
1> lists:map(fun(X) -> X * 2 end, [1, 2, 3]).
[2,4,6]
2> lists:map(fun atom_to_list/1, [a, b]).
["a","b"]
3> higher:squares([1, 2, 3]).
[1,4,9]
lists:filter
The filter function keeps the items where the fun answers true. The rest drop away.
erl
1> lists:filter(fun(X) -> X > 1 end, [1, 2, 3]).
[2,3]
2> higher:evens([1, 2, 3, 4]).
[2,4]
lists:foldl
The foldl function walks a list and carries one accumulator through every step. Your fun takes the item and the accumulator, and returns the next accumulator.
erl
1> lists:foldl(fun(X, Acc) -> Acc + X end, 0, [1, 2, 3]).
6
2> lists:foldl(fun(X, Max) -> max(X, Max) end, 0, [3, 1, 2]).
3
3> higher:total([1, 2, 3]).
6
lists:foreach
The foreach function runs a fun for every item and answers the atom ok. Use it for side effects only: printing, logging, sending.
erl
1> lists:foreach(fun(X) -> io:format("~p~n", [X]) end, [1, 2]).
1
2
ok
2> higher:shout(["Ada", "Bob"]).
Ada!
Bob!
ok
Next: Keyword lists
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
lists:map/2return?Answer
A new list with the fun applied to every item
mapbuilds a new list of the same length, one result per item, and leaves the input untouched. -
What does
lists:map(fun atom_to_list/1, [a, b])return?Answer
["a","b"]The captured
atom_to_list/1turns each atom into a string, so the result is a list of two strings. -
Which items does
lists:filter(fun(X) -> X > 1 end, [1, 2, 3])keep?Answer
[2,3]filterkeeps the items for which the fun answerstrue, and 2 and 3 are greater than 1. -
What does the fun passed to
lists:foldl/3receive?Answer
The item and the accumulator
A fold walks the list and threads a value through the fun: item first, accumulator second.
-
What does
lists:foldl(fun(X, Acc) -> Acc + X end, 0, [1, 2, 3])return?Answer
6The accumulator starts at 0 and takes on 1, then 3, then 6.
-
What does
lists:foldl(fun(X, Max) -> max(X, Max) end, 0, [3, 1, 2])return?Answer
3The accumulator holds the largest value seen so far, and 3 is the largest item.
-
What does
lists:foreach/2answer?Answer
The atom
okforeachis for side effects; it discards the results and answersok. -
When should you use
lists:foreach/2instead oflists:map/2?Answer
When the fun only has a side effect
mapbuilds a result list;foreachis the right tool when nothing needs to come back. -
What does
is_even/1in the higher module answer for4?Answer
true4 rem 2is 0, and0 =:= 0istrue. -
Why does
higher:evens/1keep working afterhigher.erlis compiled?Answer
The fun
fun is_even/1is a local capture of the module functionfun is_even/1captures the local function of the same module, solists:filter/2can call it.
The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev