Skip to content

Instantly share code, notes, and snippets.

@adolfont
Last active April 9, 2021 22:49
Show Gist options
  • Save adolfont/3f2ec229ef0cf5f11c9fd2f1766d87b7 to your computer and use it in GitHub Desktop.
Save adolfont/3f2ec229ef0cf5f11c9fd2f1766d87b7 to your computer and use it in GitHub Desktop.
Recursive anonymous functions in Elixir
f = fn
(_, 0, acc) -> acc
(g, num, acc) -> g.(g, num-1, acc+1)
end
# f.(f,10,10)
# Source: peerreynders at https://elixirforum.com/t/recursive-anonymous-functions/18421/4
"Functions are values …" Peer Reynders
# I went looking for it inspired by a message by "M G" at https://t.me/elixirES
# Fibonacci
fib = fn
(_, 0) -> 1
(_, 1) -> 1
(f, num) -> f.(f, num-1) + f.(f, num-2)
end
fib.(fib, 20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment