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.

  1. What does lists:map/2 return?

    Answer

    A new list with the fun applied to every item

    map builds a new list of the same length, one result per item, and leaves the input untouched.

  2. What does lists:map(fun atom_to_list/1, [a, b]) return?

    Answer

    ["a","b"]

    The captured atom_to_list/1 turns each atom into a string, so the result is a list of two strings.

  3. Which items does lists:filter(fun(X) -> X > 1 end, [1, 2, 3]) keep?

    Answer

    [2,3]

    filter keeps the items for which the fun answers true, and 2 and 3 are greater than 1.

  4. What does the fun passed to lists:foldl/3 receive?

    Answer

    The item and the accumulator

    A fold walks the list and threads a value through the fun: item first, accumulator second.

  5. What does lists:foldl(fun(X, Acc) -> Acc + X end, 0, [1, 2, 3]) return?

    Answer

    6

    The accumulator starts at 0 and takes on 1, then 3, then 6.

  6. What does lists:foldl(fun(X, Max) -> max(X, Max) end, 0, [3, 1, 2]) return?

    Answer

    3

    The accumulator holds the largest value seen so far, and 3 is the largest item.

  7. What does lists:foreach/2 answer?

    Answer

    The atom ok

    foreach is for side effects; it discards the results and answers ok.

  8. When should you use lists:foreach/2 instead of lists:map/2?

    Answer

    When the fun only has a side effect

    map builds a result list; foreach is the right tool when nothing needs to come back.

  9. What does is_even/1 in the higher module answer for 4?

    Answer

    true

    4 rem 2 is 0, and 0 =:= 0 is true.

  10. Why does higher:evens/1 keep working after higher.erl is compiled?

    Answer

    The fun fun is_even/1 is a local capture of the module function

    fun is_even/1 captures the local function of the same module, so lists:filter/2 can 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

Banner image