<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">Thanks both, <br>
<br>
Rearraging to code to this : <br>
<br>
-module(sum_recursion).<br>
<br>
-export([sum/1, sum/2]).<br>
<br>
% when the number is zero the outcome will also be zero<br>
sum(0) -><br>
0; <br>
<br>
% when a number is greater then 0 , call the helper function.<br>
sum(End) when End > 0 -> <br>
sum_acc(End,0).<br>
<br>
% when the first number is greater then zero and lower then the
second number<br>
sum(Begin, End) when Begin >= 0 , Begin =< End -><br>
sum_acc(Begin, End, Begin).<br>
<br>
% When End is equal to zero all the numbers are added<br>
sum_acc(0,Acc) -><br>
Acc;<br>
<br>
% when end is not zero there are more numbers to be added. <br>
sum_acc(End, Acc) -><br>
sum_acc(End - 1, Acc + End).<br>
<br>
% When End is equal to Begin all numbers are added. <br>
sum_acc(Begin, Begin, Acc) -><br>
Acc ; <br>
<br>
% When end is not equal to begin then there are more numbers to be
added. <br>
sum_acc(Begin, End, Acc) -><br>
sum_acc(Begin, End - 1, Acc + End).<br>
<br>
and it all works fine. <br>
<br>
<br>
Bengt Kleberg schreef op 10-2-2015 om 9:06:<br>
</div>
<blockquote cite="mid:54D9BC1D.7060800@ericsson.com" type="cite">
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
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.<br>
<br>
<br>
bengt<br>
<br>
<div class="moz-cite-prefix">On 02/10/2015 09:01 AM, Roelof Wobben
wrote:<br>
</div>
<blockquote cite="mid:54D9BAED.8050406@home.nl" type="cite">
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
Hello, <br>
<br>
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 <br>
<br>
I now have this : <br>
<br>
-module(sum_recursion).<br>
<br>
-export([sum/1, sum/2]).<br>
<br>
% when the number is zero the outcome will also be zero<br>
sum(0) -><br>
0; <br>
<br>
% when a number is greater then 0 , call the helper function.<br>
sum(End) when End > 0 -> <br>
sum_acc(End,0);<br>
<br>
% when the first number is greater then zero and lower then the
second number<br>
sum(Begin, End) when Begin >= 0 , Begin =< End -><br>
sum_acc(Begin, End, Begin).<br>
<br>
% When End is equal to zero all the numbers are added<br>
sum_acc(0,Acc) -><br>
Acc;<br>
<br>
% When End is equal to Begin all numbers are added. <br>
sum_acc(Begin, Begin, Acc) -><br>
Acc ; <br>
<br>
% when end is not zero there are more numbers to be added. <br>
sum_acc(End, Acc) -><br>
sum_acc(End - 1, Acc + End); <br>
<br>
% When end is not equal to begin then there are more numbers to
be added. <br>
sum_acc(Begin, End, Acc) -><br>
sum_acc(Begin, End - 1, Acc + End).<br>
<br>
<br>
but as soon as I try to compile it I see this error : <br>
<br>
<input class="terminal-input">
<div data-row="1" class="terminal-row">error </div>
<div data-row="2" class="terminal-row">7> c(sum_recursion). </div>
<div data-row="3" class="terminal-row">sum_recursion.erl:14: head mismatch </div>
<div data-row="4" class="terminal-row">sum_recursion.erl:22: head mismatch </div>
<div data-row="5" class="terminal-row">sum_recursion.erl:3: function sum/1 undefined </div>
sum_recursion.erl:3: function sum/2 undefined <br>
<br>
Am I not alllowed to do this <br>
<br>
sum(x) -> <br>
x ; <br>
<br>
sum(x,y) -><br>
x + y. <br>
<br>
or is there something else missing ?<br>
<br>
Roelof<br>
<br>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
erlang-questions mailing list
<a moz-do-not-send="true" class="moz-txt-link-abbreviated" href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a>
<a moz-do-not-send="true" class="moz-txt-link-freetext" href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a>
</pre>
</blockquote>
<br>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
erlang-questions mailing list
<a class="moz-txt-link-abbreviated" href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a>
<a class="moz-txt-link-freetext" href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a>
</pre>
</blockquote>
<br>
</body>
</html>