Last active
April 9, 2021 22:49
-
-
Save adolfont/3f2ec229ef0cf5f11c9fd2f1766d87b7 to your computer and use it in GitHub Desktop.
Recursive anonymous functions in Elixir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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