Submitted by flabberghaster in just_post
musou wrote (edited )
it doesn't let you write a recursive function the normal way, instead you write a function that takes a function as input (so you can introudce a stand-in name for the recursive call) and returns the function you want to write recursively except you call the input function in place of the recursive step. then you pass that through the y combinator to get it to work. here's an untested example in elixir because it's what i think in
non_recursive_factorial = fn f ->
fn
0 -> 1
n -> n * f.(n - 1)
end
end
# this will just return a useless function, not the answer. :[
non_recursive_factorial.(5)
# gotta run it through the y combinator.
recursive_factorial = y_combinator.(non_recursive_factorial)
# this should return 120
recursive_factorial.(5)
musou wrote
and if you actually want to try it here's a spagehti i found on rosetta code for the y combinator
y_combinator = fn f ->
(fn x ->
x.(x)
end).(fn y ->
f.(fn z ->
y.(y).(z)
end)
end)
end
i went back and actually did try it and it works using this definition
Viewing a single comment thread. View all comments