Calling a Function with Same argument twice

sapan shah sapan.gcet@REDACTED
Wed Oct 14 15:09:12 CEST 2009


Hi,

see the code below.
%%%%%%%%%%
-module(temp).
-compile(export_all).


start(X) ->
    put(test, 0),
    case X of 1 -> test(2, 2); 2-> test1(2) end,
    get(test).


test(0,0) ->
    put(test, get(test) +1),
    {0,0};

test(X,Y) ->
    {element(1,test(X-1,Y-1)),element(2,test(X-1,Y-1))}.


test1(0) ->
    put(test, get(test) +1),
    {0};
test1(X) ->
    {element(1,test1(X-1))}.
%%%%%%%

We call temp:start(1), the output is 4. The output should have been 1.
Here, when start(1) is called, it intern calls
{element(1, test(1,1), element(2, test(1,1)}

The first call of test(1,1) should recursively evaluate test(1,1) while the
second call should already use the value returned by the first call.. Is
this a right thinking???
-- 
Regards,
Sapan Shah


More information about the erlang-questions mailing list