Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang function already defined with guard clauses

Tags:

erlang

Writing a recursive function, I want one fn that executes when the list has elements and another when it is empty:

 transfer(Master, [H|Tail]) ->
  Master ! {transfer, H},
  transfer(Master, Tail).

transfer(_Master, []) ->
  nil.

The problem I'm getting is src/redis/redis_worker.erl:13: function transfer/2 already defined. I understand it is upset about two functions with the same name and arity, but these two should be different.

like image 847
Chris Avatar asked Oct 10 '12 17:10

Chris


2 Answers

The problem is that clauses of a function need to be separated by a semicolon instead of a period.

transfer(Master, [H|Tail]) ->
    Master ! {transfer, H},
    transfer(Master, Tail); % use semicolon here, not period

transfer(_Master, []) ->
    nil.

When you use a period to terminate the clause, the compiler considers that function's definition to be complete, so it sees your code as two separate functions instead of different clauses of the same function.

See the Erlang reference for Function Declaration Syntax for more details.

like image 173
Tadmas Avatar answered Nov 10 '22 19:11

Tadmas


You need to use a semicolon instead of a colon to separate the two function clauses.

like image 36
3lectrologos Avatar answered Nov 10 '22 19:11

3lectrologos