[erlang-questions] head mismatch

Bengt Kleberg <>
Tue Feb 10 09:06:53 CET 2015


You are correct it is nopt possibler to have sum/1 followed by sum/2 
with an ";" between them. They are different functions that happen to 
have the same name. But since the arity (number of arguments) is 
different they are different.


bengt

On 02/10/2015 09:01 AM, Roelof Wobben wrote:
> Hello,
>
> I changed my code so that it also can count from a number to a number, 
> so sum(3,6) does 3+4+5+6 and sum(2) does 0+1+2
>
> I now have this :
>
> -module(sum_recursion).
>
> -export([sum/1, sum/2]).
>
> % when the number is zero the outcome will also be zero
> sum(0) ->
>   0;
>
> % when a number is greater then 0 , call the helper function.
> sum(End) when End > 0 ->
>   sum_acc(End,0);
>
> % when the first number is greater then zero and lower then the second 
> number
> sum(Begin, End) when Begin >= 0 , Begin =< End ->
>   sum_acc(Begin, End, Begin).
>
> % When End is equal to zero all the numbers are added
> sum_acc(0,Acc) ->
>      Acc;
>
> % When End is equal to Begin all numbers are added.
> sum_acc(Begin, Begin, Acc) ->
>   Acc ;
>
> % when end is not zero there are more numbers to be added.
> sum_acc(End, Acc) ->
>      sum_acc(End - 1, Acc + End);
>
> % When end is not equal to begin then there are more numbers to be added.
> sum_acc(Begin, End, Acc) ->
>   sum_acc(Begin, End - 1, Acc + End).
>
>
> but as soon as I try to compile it I see this error :
>
> error
> 7> c(sum_recursion).
> sum_recursion.erl:14: head mismatch
> sum_recursion.erl:22: head mismatch
> sum_recursion.erl:3: function sum/1 undefined
> sum_recursion.erl:3: function sum/2 undefined
>
> Am I not alllowed to do this
>
> sum(x) ->
>    x ;
>
> sum(x,y) ->
>   x + y.
>
> or is there something else missing ?
>
> Roelof
>
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150210/fad54c03/attachment.html>


More information about the erlang-questions mailing list