Function as an argument in erlang

I am trying to do something like this:

  -module(count).
  -export([main/0]).


  sum(X, Sum) -> X + Sum.
  main() ->
    lists:foldl(sum, 0, [1,2,3,4,5]).

but see warning and code:

function sum/2 is unused

How to fix the code?

NB: this is just a sample that illustrates the problem, so there is no reason to suggest a solution that uses fun-expression.

+4
source share
1 answer

Erlang has a slightly more explicit syntax for this:

-module(count).
-export([main/0]).

sum(X, Sum) -> X + Sum.
main() ->
    lists:foldl(fun sum/2, 0, [1,2,3,4,5]).

See also " Find out what you have Erlang ":

If function names are written without a parameter list, then these names are interpreted as atoms, and atoms cannot be functions, so the call is not made.

...

, . , Module: Function/Arity: , .

+8

All Articles