Skip to content

First-class function can't call itself #7036

Closed
@thinkpad20

Description

@thinkpad20

I'm not sure if this is an issue, but it sure seems to be. The following code:

fn main() {
    let collatz = |i: int, acc: int| -> int {
        if i == 1 { acc }
        else if i % 2 == 0 { collatz(i/2, acc + 1) }
        else { collatz(3*i + 1, acc + 1) }
    };
    println(fmt!("collatz(400) = %d", collatz(400, 0)));
}

Produces the following error on compilation:

> rustc collatz.rs
recurse.rs:4:23: 4:30 error: unresolved name: `collatz`.
recurse.rs:4        else if i % 2 == 0 { collatz(i/2, acc + 1) }
                                         ^~~~~~~
recurse.rs:5:9: 5:16 error: unresolved name: `collatz`.
recurse.rs:5        else { collatz(3*i + 1, acc + 1) }
                           ^~~~~~~
error: aborting due to 2 previous errors

Why is the function not aware of its own name?

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