Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Primitive recursion #25

Closed
WhiteBlackGoose opened this issue Jun 18, 2022 · 3 comments
Closed

Primitive recursion #25

WhiteBlackGoose opened this issue Jun 18, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@WhiteBlackGoose
Copy link
Member

WhiteBlackGoose commented Jun 18, 2022

For recursion for anonymous functions:

var fact = Prec((int i, Func<int, int> fact)
        => i == 0 ? 1 : i * fact(i - 1)
);

PoC implementation:

static Func<TIn, TOut> Prec<TIn, TOut>(Func<TIn, Func<TIn, TOut>, TOut> rec)
{
    TOut Hehe(TIn input)
    {
        return rec(input, static i => Hehe(i)); // do not eta reduce!
    }
    
    return Hehe;
}

Together with memoization, we could anonymously define, say, fibonacci:

var fib = Mem(Prec(int i, Func<int, int> fib) => i switch
    0 or 1 => 1,
    var n => fib(i - 1) + fib(i - 2)
}));
@WhiteBlackGoose WhiteBlackGoose added the enhancement New feature or request label Jun 18, 2022
@WhiteBlackGoose
Copy link
Member Author

Full impl of both

@WhiteBlackGoose
Copy link
Member Author

(it doesn't work btw, we will have to merge mem and prec into ... mrec?)

@WhiteBlackGoose
Copy link
Member Author

Basically dup of #23

@WhiteBlackGoose WhiteBlackGoose closed this as not planned Won't fix, can't repro, duplicate, stale Jun 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant