Peano Arithmetic

Michael Haufe TNO at TheNewObjective.com
Thu Aug 14 22:22:30 PDT 2008


Good evening,

I apologize if this issue was discussed already, I've been unable to 
find an answer after a few hours of digging and Googling.

I was watching some old SICP videos and translating some of the code 
into JavaScript to see how it compared to Scheme and came across a 
couple of errors when implementing Peano Arithmetic.

Each of these functions should are expected to return 4 if I pass 2,2 
into them:
----------------------------------------------------------------------------------
            function add1(x,y){
                if(!x){
                    return y;
                }
                else{
                    x--;y++;
                    return add1(x,y);
                };
            }

            function add2(x,y){
                if(!x){
                    return y;
                }
                else{
                    return add2(x--,y++); //recursion error
                };
            }
            function add3(x,y){
                if(!x){
                    return y;
                }
                else{
                    x--;
                    return add3(x,y)++; //error: cannot assign to a 
function result
                }
            }
----------------------------------------------------------------------------------

The first function works as expected.
The 2nd function goes into an infinite loop since the variables are 
apparently not assigned as expected before passed into the function
The 3rd function  throws an assignment error once it reaches the return.

So my question is whether this behavior is by design or by accident?





More information about the Es-discuss mailing list