Maria
Maria

Reputation: 575

Syntax error before '->' for no apparent reason

I am defining some functions in an Erlang script file and calling them in main to demonstrate their correctness. I have not had any problems up to now, but suddenly I'm getting an error for no real reason. Here is the code in question (I have checked that this line is the problem by commenting it out):

fibSeq() -> [0] ++ [1] ++ lists:zipwith(func(X, Y) -> X + Y end, fibs(), lists:delete(0, fibSeq())).

The idea behind this function is to efficiently calculate the Fibonacci sequence. It's possible the error is arising because of the infinite recursive nature of the function, however I believe I read that Erlang uses lazy evaluation, so I feel like this should work.

Edit: The usage of this would be list:sublist(fibSeq(), N) or list:nth(N, fibSeq()) where N is an integer.

Edit 2: The error message is "Syntax error before '->'" with reference to the line number one above the fibSeq() function, and the code before it is

merge([], []) -> [];
merge(A, []) -> A;
merge([], B) -> B;
merge([A|As], [B|Bs]) when A < B -> [A] ++ merge(As, [B] ++ Bs);
merge([A|As], [B|Bs]) -> [B] ++ merge([A] ++ As, Bs).

mergesort([]) -> [];
mergesort([A]) -> [A];
mergesort(As) ->
    merge(mergesort(lists:sublist(As, length(As) div 2)), mergesort(lists:sublist(As, length(As) div 2 + 1, length(As) div 2 + 1))).

I have changed my fibonacci code to use a different linear evaluation that I thought of shortly after:

fib(N) when N >= 0, is_integer(N) ->  fibHelp(0, 1, N).

fibHelp(L, _, 0) -> L;
fibHelp(L, H, A) when A > 0, is_integer(L), is_integer(H), is_integer(A) ->
    fibHelp(H, L+H, A - 1).

Upvotes: 1

Views: 734

Answers (1)

Patrik Oldsberg
Patrik Oldsberg

Reputation: 1550

The syntax for higher order functions in erlang is fun(X) -> X * 2 end. Using func is a syntax error.

Upvotes: 4

Related Questions