Skip to content

Instantly share code, notes, and snippets.

@sgrif
Created November 21, 2013 01:23
Show Gist options
  • Save sgrif/7574451 to your computer and use it in GitHub Desktop.
Save sgrif/7574451 to your computer and use it in GitHub Desktop.
Z Combinator in elixir
z = fn
f -> fn
x -> f.(fn y -> x.(x).(y) end)
end.(fn
x -> f.(fn y -> x.(x).(y) end)
end)
end
add_squares = z.(fn
f -> fn
[n|ns] -> n*n + f.(ns)
[] -> 0
end
end)
IO.puts "#{add_squares.([1, 2, 5])}"
@sgrif
Copy link
Author

sgrif commented Nov 21, 2013

If you want the same name inside the function, this would also work.

add_squares = z.(fn
  add_squares -> fn
    [n|ns] -> n*n + add_squares.(ns)
    []     -> 0
  end
end)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment