Peano Arithmetic

Brendan Eich brendan at mozilla.org
Thu Aug 14 23:04:26 PDT 2008


On Aug 14, 2008, at 10:22 PM, Michael Haufe wrote:

>             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

You want --x and ++y as the arguments to add2 -- pre-decrement and  
pre-increment, not post-.


> The 3rd function  throws an assignment error once it reaches the  
> return.

Functions return rvalues, not lvalues, to use the C jargon.


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

Design.

/be




More information about the Es-discuss mailing list