Peano Arithmetic

# Michael Haufe (17 years ago)

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?

# Brendan Eich (17 years ago)
# Jason Orendorff (17 years ago)

On Fri, Aug 15, 2008 at 12:22 AM, Michael Haufe <TNO at thenewobjective.com> wrote:

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.

I think it's a case of mistranslation. When Scheme says (+ x 1), the right ECMAScript translation is x + 1, not x++. So, in your examples:

return add2(x--,y++); //recursion error

return add2(x - 1, y + 1);

return add3(x,y)++; //error: cannot assign to a function result

return add3(x, y) + 1;

Incidentally, ECMAScript does not have Scheme-like tail recursion, so the examples from SICP will not all necessarily work.

# Michael Haufe (17 years ago)

Jason Orendorff wrote:

On Fri, Aug 15, 2008 at 12:22 AM, Michael Haufe <TNO at thenewobjective.com> wrote:

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.

I think it's a case of mistranslation. When Scheme says (+ x 1), the right ECMAScript translation is x + 1, not x++. So, in your examples:

return add2(x--,y++); //recursion error

return add2(x - 1, y + 1);

return add3(x,y)++; //error: cannot assign to a function result

return add3(x, y) + 1;

Incidentally, ECMAScript does not have Scheme-like tail recursion, so the examples from SICP will not all necessarily work.

-j

@Brendan: Thanks for the corrections and the reference to rvalues and lvalues, it sheds light on quite a few things since I was able to find quite a bit of documentation on the terms.

@Jason: Its from the 2nd SICP video (1b) at 14 minutes:


(define (+ x y) (if (= x 0) y (+ (-1+ x) (1+ y))))

function add(x,y) (!x) ? y : add(--x,++y);

I don't claim to be a Scheme expert, but I believe my interpretation to be correct. I am also aware of the recursion limits of ES having used it for a number of years. This is for personal study and amusement.

# Maciej Stachowiak (17 years ago)

On Aug 15, 2008, at 9:17 PM, Michael Haufe wrote:


(define (+ x y) (if (= x 0) y (+ (-1+ x) (1+ y))))

function add(x,y) (!x) ? y : add(--x,++y);

I don't claim to be a Scheme expert, but I believe my interpretation
to be correct. I am also aware of the recursion limits of ES having used it for a
number of years. This is for personal study and amusement.

I do claim to be a Scheme expert, and I believe your interpretation is
incorrect. The -1+ and 1+ procedures in Scheme return a value derived
by adding either -1 or +1 to their argument, but do not mutate their
argument - a Scheme procedure cannot do so, only a special form could.
However, ECMAScript -- and ++ mutate the referenced location, in
addition to returning a value. Preincrement/predecrement operators
will at least return the modified instead of the original value,
unlike the post versions, but they are still mutators and thus do not
match the Scheme seantics.

, Maciej

# Michael Haufe (17 years ago)

Maciej Stachowiak wrote:

On Aug 15, 2008, at 9:17 PM, Michael Haufe wrote:


(define (+ x y) (if (= x 0) y (+ (-1+ x) (1+ y))))

function add(x,y) (!x) ? y : add(--x,++y);

I don't claim to be a Scheme expert, but I believe my interpretation to be correct. I am also aware of the recursion limits of ES having used it for a number of years. This is for personal study and amusement.

I do claim to be a Scheme expert, and I believe your interpretation is incorrect. The -1+ and 1+ procedures in Scheme return a value derived by adding either -1 or +1 to their argument, but do not mutate their argument - a Scheme procedure cannot do so, only a special form could. However, ECMAScript -- and ++ mutate the referenced location, in addition to returning a value. Preincrement/predecrement operators will at least return the modified instead of the original value, unlike the post versions, but they are still mutators and thus do not match the Scheme seantics.

, Maciej

Then I stand corrected, much appreciated.