Erlang getting error ** 1: syntax error before: '->' **

前端 未结 3 1011
轻奢々
轻奢々 2021-02-07 05:59

I have started some hands on in Erlang and I am getting : ** 1: syntax error before: \'->\' ** whenever i am declaring any function for eg. to calculate sum of a

3条回答
  •  萌比男神i
    2021-02-07 06:33

    You can't define functions in the shell using the same syntax as in an erl file.

    You can define fun's though.

    Syntax in the shell needs to be:

    Sum = fun([], _) -> 0; ([H | T], F) -> H + F(T, F) end,
    Sum([1,2,3], Sum).
    

    Note that recursive anonymous functions (which this is) are defined in an ugly way. You basically have to pass the function as an argument to itself.

提交回复
热议问题