Search code examples
haskellerlangcurryingfold

Currying Functions Erlang


I'm trying to redo all of my Haskell homework problems using Erlang, and one thing that gets me is how to use a list of functions that don't have all of their parameters.

Example: I'm trying to use this fold, but I don't know how to pass in the functions so that it operates on the accumulator

%%inside my module)
add(X,Y) -> X + Y.

multiply(X,Y) -> X*Y.

Afterwards using this in the command line:

lists:foldl(fun(Function,Accumulator) -> Function(Accumulator) end, 3, [add(3),multiply(5)]).

Solution

  • In Erlang you must call function passing all parameters it requires. But you can easily avoid it by creating an anonymous function which takes only those parameters you need and then calls your function rightly. If you need a function which takes one parameter X and calls function add(3, X) you can create an anonymous function like that:

    fun (X) -> add(3, X) end
    

    This is an example for your task:

    lists:foldl(fun (Function, Accumulator) -> Function(Accumulator) end, 3,
        [fun (X) -> add(3, X) end, fun (X) -> multiply(5, X) end]).