Skip to content

Can you write this recursive function? #61

@masuhar

Description

@masuhar
function add(n) {
    if (n==0)     return {message: "end"};
    else return add(n-1);
    
}
let x = add(3);

This is a correct JavaScript program. However, many of you cannot write this in Kanon. Hint: if it were like this, you can write without problems.

function add(n) {
    if (n>0) return add(n-1);
    else     return {message: "end"};
    
}
let x = add(3);

The problem is that there is a partially written program like this

function add(n) {
    if (n==0)     return {message: "end"};
    else return add//<--insertion point is here. you will add "(n-1);" from this point.
    
}
let x = add(3);

Then, Kanon executes the code; it falls into the else branch; it calls "add" with no argument; JavaScript evaluates the body of "add" with n=undefined! (Did you know that?) "n==0" evaluates to undefined; the else branch is chosen and it calls "add" with no argument again.

To summarize, you can easily write an infinite recursive function in JS. Kanon does not seem to detect infinite recursive calls despite it does infinite loops.

Actions:

Sub-issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions