Peano Arithmetic

Michael Haufe TNO at TheNewObjective.com
Fri Aug 15 21:17:57 PDT 2008


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.




More information about the Es-discuss mailing list