Skip to content

Instantly share code, notes, and snippets.

@emadb emadb/fizz_buzz.exs
Last active Aug 8, 2019

Embed
What would you like to do?
FizzBuzz in Elixir using only functions: no numbers, no strings, no loops and no ifs!
defmodule Church do
def fizzbuzz do
(fn k -> fn f -> ((fn f -> (fn x -> f.(fn y -> x.(x).(y) end) end).(fn x -> f.(fn y -> x.(x).(y) end) end) end).(
fn f -> fn l -> fn x -> fn g ->
(fn b -> b end).((fn p -> p.(fn a -> fn b -> a end end) end).(l)).(x).(fn y -> g.(f.((fn l -> (fn p -> p.(fn a -> fn b -> b end end) end)
.((fn p -> p.(fn a -> fn b -> b end end) end).(l)) end).(l)).(x).(g)).((fn l -> (fn p -> p.(fn a -> fn b -> a end end) end)
.((fn p -> p.(fn a -> fn b -> b end end) end).(l)) end).(l)).(y) end)
end end end end
)).(k).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(fn x -> (fn y -> x end) end).(fn x -> (fn y -> y end) end))
.(fn l -> fn x -> (fn l -> fn x -> (fn x -> fn y -> fn f -> f.(x).(y) end end end).(fn x -> (fn y -> y end) end).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(x).(l)) end end).(l).(f.(x)) end end ) end end)
.(((fn f -> (fn x -> f.(fn y -> x.(x).(y) end) end).(fn x -> f.(fn y -> x.(x).(y) end) end) end).(
fn f -> fn m -> fn n ->
(fn b -> b end).((fn m -> (fn n ->
(fn n -> n.(fn x -> (fn x -> (fn y -> y end) end) end).(fn x -> (fn y -> x end) end) end)
.((fn m -> fn n -> n.(fn n -> fn f -> fn x -> (
n.(fn g -> (fn h -> h.(g.(f)) end ) end).(fn y -> x end).(fn y -> y end) )
end end end).(m) end end).(m).(n)) end) end).(m).(n)).(
fn x ->
(fn l -> fn x -> (fn x -> fn y -> fn f -> f.(x).(y) end end end).(fn x -> (fn y -> y end) end).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(x).(l)) end end)
.(f.((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).(m)).(n)).(m).(x)
end
).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(fn x -> (fn y -> x end) end).(fn x -> (fn y -> y end) end))
end end end
))
.(fn p -> (fn x -> p.(x) end) end)
.(fn p -> (fn x -> p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(x)))))))))))))))))))) end) end)).(fn n ->
(fn b -> b end).((fn n -> n.(fn x -> (fn x -> (fn y -> y end) end) end).(fn x -> (fn y -> x end) end) end).(((fn f -> (fn x -> f.(fn y -> x.(x).(y) end) end).(fn x -> f.(fn y -> x.(x).(y) end) end) end).(fn f -> (fn m -> (fn n ->
(fn b -> b end).((fn m -> (fn n ->
(fn n -> n.(fn x -> (fn x -> (fn y -> y end) end) end).(fn x -> (fn y -> x end) end) end)
.((fn m -> fn n -> n.(fn n -> fn f -> fn x -> (
n.(fn g -> (fn h -> h.(g.(f)) end ) end).(fn y -> x end).(fn y -> y end) )
end end end).(m) end end).(m).(n)) end) end).(n).(m)).(fn x -> f.((fn m -> fn n -> n.(fn n -> fn f -> fn x -> (
n.(fn g -> (fn h -> h.(g.(f)) end ) end).(fn y -> x end).(fn y -> y end) )
end end end).(m) end end).(m).(n)).(n).(x) end).(m)
end) end) end)).(n).(fn p -> (fn x -> p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(x))))))))))))))) end) end)))
.((fn l -> fn x -> (fn x -> fn y -> fn f -> f.(x).(y) end end end).(fn x -> (fn y -> y end) end).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(x).(l)) end end)
.((fn l -> fn x -> (fn x -> fn y -> fn f -> f.(x).(y) end end end).(fn x -> (fn y -> y end) end).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(x).(l)) end end)
.((fn l -> fn x -> (fn x -> fn y -> fn f -> f.(x).(y) end end end).(fn x -> (fn y -> y end) end).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(x).(l)) end end)
.((fn l -> fn x -> (fn x -> fn y -> fn f -> f.(x).(y) end end end).(fn x -> (fn y -> y end) end).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(x).(l)) end end)
.((fn l -> fn x -> (fn x -> fn y -> fn f -> f.(x).(y) end end end).(fn x -> (fn y -> y end) end).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(x).(l)) end end)
.((fn l -> fn x -> (fn x -> fn y -> fn f -> f.(x).(y) end end end).(fn x -> (fn y -> y end) end).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(x).(l)) end end)
.((fn l -> fn x -> (fn x -> fn y -> fn f -> f.(x).(y) end end end).(fn x -> (fn y -> y end) end).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(x).(l)) end end)
.((fn l -> fn x -> (fn x -> fn y -> fn f -> f.(x).(y) end end end).(fn x -> (fn y -> y end) end).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(x).(l)) end end)
.((fn x -> fn y -> fn f -> f.(x).(y) end end end).(fn x -> (fn y -> x end) end).(fn x -> (fn y -> y end) end))
.((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).(fn p -> (fn x -> p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(x)))))))))) end) end))))))
.((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).(fn p -> (fn x -> p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(x)))))))))) end) end))))))
.((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).(fn p -> (fn x -> p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(x)))))))))) end) end)))))
.(fn p -> (fn x -> p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(x)))))))))) end) end))
.((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).(fn p -> (fn x -> p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(x)))))))))) end) end))))))
.((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).(fn p -> (fn x -> p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(x)))))))))) end) end))))))
.((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).(fn p -> (fn x -> p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(x)))))))))) end) end))))
.((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).(fn p -> (fn x -> p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(x)))))))))) end) end)))
.((fn b -> b end).((fn n -> n.(fn x -> (fn x -> (fn y -> y end) end) end).(fn x -> (fn y -> x end) end) end).(((fn f -> (fn x -> f.(fn y -> x.(x).(y) end) end).(fn x -> f.(fn y -> x.(x).(y) end) end) end).(fn f -> (fn m -> (fn n ->
(fn b -> b end).((fn m -> (fn n ->
(fn n -> n.(fn x -> (fn x -> (fn y -> y end) end) end).(fn x -> (fn y -> x end) end) end)
.((fn m -> fn n -> n.(fn n -> fn f -> fn x -> (
n.(fn g -> (fn h -> h.(g.(f)) end ) end).(fn y -> x end).(fn y -> y end) )
end end end).(m) end end).(m).(n)) end) end).(n).(m)).(fn x -> f.((fn m -> fn n -> n.(fn n -> fn f -> fn x -> (
n.(fn g -> (fn h -> h.(g.(f)) end ) end).(fn y -> x end).(fn y -> y end) )
end end end).(m) end end).(m).(n)).(n).(x) end).(m)
end) end) end)).(n).(fn p -> (fn x -> p.(p.(p.(x))) end) end))).((fn l -> fn x -> (fn x -> fn y -> fn f -> f.(x).(y) end end end).(fn x -> (fn y -> y end) end)
.((fn x -> fn y -> fn f -> f.(x).(y) end end end).(x).(l)) end end).((fn l -> fn x -> (fn x -> fn y -> fn f -> f.(x).(y) end end end)
.(fn x -> (fn y -> y end) end).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(x).(l)) end end).((fn l -> fn x -> (fn x -> fn y -> fn f -> f.(x).(y) end end end)
.(fn x -> (fn y -> y end) end).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(x).(l)) end end).((fn l -> fn x -> (fn x -> fn y -> fn f -> f.(x).(y) end end end)
.(fn x -> (fn y -> y end) end).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(x).(l)) end end).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(fn x -> (fn y -> x end) end).(fn x -> (fn y -> y end) end))
.((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).(fn p -> (fn x -> p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(x)))))))))) end) end))))))
.((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).(fn p -> (fn x -> p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(x)))))))))) end) end))))))
.((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).(fn p -> (fn x -> p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(x)))))))))) end) end))))
.((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).(fn p -> (fn x -> p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(x)))))))))) end) end)))
.((fn b -> b end).((fn n -> n.(fn x -> (fn x -> (fn y -> y end) end) end).(fn x -> (fn y -> x end) end) end).(((fn f -> (fn x -> f.(fn y -> x.(x).(y) end) end).(fn x -> f.(fn y -> x.(x).(y) end) end) end).(fn f -> (fn m -> (fn n ->
(fn b -> b end).((fn m -> (fn n ->
(fn n -> n.(fn x -> (fn x -> (fn y -> y end) end) end).(fn x -> (fn y -> x end) end) end)
.((fn m -> fn n -> n.(fn n -> fn f -> fn x -> (
n.(fn g -> (fn h -> h.(g.(f)) end ) end).(fn y -> x end).(fn y -> y end) )
end end end).(m) end end).(m).(n)) end) end).(n).(m)).(fn x -> f.((fn m -> fn n -> n.(fn n -> fn f -> fn x -> (
n.(fn g -> (fn h -> h.(g.(f)) end ) end).(fn y -> x end).(fn y -> y end) )
end end end).(m) end end).(m).(n)).(n).(x) end).(m)
end) end) end)).(n).(fn p -> (fn x -> p.(p.(p.(p.(p.(x))))) end) end))).((fn l -> fn x -> (fn x -> fn y -> fn f -> f.(x).(y) end end end).(fn x -> (fn y -> y end) end).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(x).(l)) end end)
.((fn l -> fn x -> (fn x -> fn y -> fn f -> f.(x).(y) end end end).(fn x -> (fn y -> y end) end).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(x).(l)) end end)
.((fn l -> fn x -> (fn x -> fn y -> fn f -> f.(x).(y) end end end).(fn x -> (fn y -> y end) end).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(x).(l)) end end)
.((fn l -> fn x -> (fn x -> fn y -> fn f -> f.(x).(y) end end end).(fn x -> (fn y -> y end) end).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(x).(l)) end end)
.((fn x -> fn y -> fn f -> f.(x).(y) end end end).(fn x -> (fn y -> x end) end).(fn x -> (fn y -> y end) end))
.((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).(fn p -> (fn x -> p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(x)))))))))) end) end))))))
.((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).(fn p -> (fn x -> p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(x)))))))))) end) end))))))
.((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).((fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end).(fn p -> (fn x -> p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(x)))))))))) end) end)))))
.(fn p -> (fn x -> p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(x)))))))))) end) end))
.(((fn f -> (fn x -> f.(fn y -> x.(x).(y) end) end).(fn x -> f.(fn y -> x.(x).(y) end) end) end).(fn f ->
fn n ->
(fn l -> fn x -> ((fn f -> (fn x -> f.(fn y -> x.(x).(y) end) end).(fn x -> f.(fn y -> x.(x).(y) end) end) end).(
fn f -> fn l -> fn x -> fn g ->
(fn b -> b end).((fn p -> p.(fn a -> fn b -> a end end) end).(l)).(x).(fn y -> g.(f.((fn l -> (fn p -> p.(fn a -> fn b -> b end end) end)
.((fn p -> p.(fn a -> fn b -> b end end) end).(l)) end).(l)).(x).(g)).((fn l -> (fn p -> p.(fn a -> fn b -> a end end) end)
.((fn p -> p.(fn a -> fn b -> b end end) end).(l)) end).(l)).(y) end)
end end end end
)).(l).((fn l -> fn x -> (fn x -> fn y -> fn f -> f.(x).(y) end end end)
.(fn x -> (fn y -> y end) end).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(x).(l)) end end).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(fn x -> (fn y -> x end) end).(fn x -> (fn y -> y end) end))
.(x)).(fn l -> fn x -> (fn x -> fn y -> fn f -> f.(x).(y) end end end).(fn x -> (fn y -> y end) end).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(x).(l)) end end) end end)
.(fn b -> b end.((fn m -> (fn n ->
(fn n -> n.(fn x -> (fn x -> (fn y -> y end) end) end).(fn x -> (fn y -> x end) end) end)
.((fn m -> fn n -> n.(fn n -> fn f -> fn x -> (
n.(fn g -> (fn h -> h.(g.(f)) end ) end).(fn y -> x end).(fn y -> y end) )
end end end).(m) end end).(m).(n)) end) end).(n).((fn n -> fn f -> fn x -> (
n.(fn g -> (fn h -> h.(g.(f)) end ) end).(fn y -> x end).(fn y -> y end) )
end end end).(fn p -> (fn x -> p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(x)))))))))) end) end))).((fn x -> fn y -> fn f -> f.(x).(y) end end end).(fn x -> (fn y -> x end) end).(fn x -> (fn y -> y end) end))
.(fn x -> f.((fn f -> (fn x -> f.(fn y -> x.(x).(y) end) end).(fn x -> f.(fn y -> x.(x).(y) end) end) end).(fn f -> fn m -> fn n ->
(fn b -> b end).((fn m -> (fn n ->
(fn n -> n.(fn x -> (fn x -> (fn y -> y end) end) end).(fn x -> (fn y -> x end) end) end)
.((fn m -> fn n -> n.(fn n -> fn f -> fn x -> (
n.(fn g -> (fn h -> h.(g.(f)) end ) end).(fn y -> x end).(fn y -> y end) )
end end end).(m) end end).(m).(n)) end) end).(n).(m)).(fn x -> (fn n -> (fn p -> (fn x -> p.(n.(p).(x)) end) end ) end)
.(f.((fn m -> fn n -> n.(fn n -> fn f -> fn x -> (
n.(fn g -> (fn h -> h.(g.(f)) end ) end).(fn y -> x end).(fn y -> y end) )
end end end).(m) end end).(m).(n)).(n)).(x) end).(fn p -> (fn x -> x end) end)
end end end
).(n).(fn p -> (fn x -> p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(x)))))))))) end) end)).(x) end))
.(((fn f -> (fn x -> f.(fn y -> x.(x).(y) end) end).(fn x -> f.(fn y -> x.(x).(y) end) end) end).(fn f -> (fn m -> (fn n ->
(fn b -> b end).((fn m -> (fn n ->
(fn n -> n.(fn x -> (fn x -> (fn y -> y end) end) end).(fn x -> (fn y -> x end) end) end)
.((fn m -> fn n -> n.(fn n -> fn f -> fn x -> (
n.(fn g -> (fn h -> h.(g.(f)) end ) end).(fn y -> x end).(fn y -> y end) )
end end end).(m) end end).(m).(n)) end) end).(n).(m)).(fn x -> f.((fn m -> fn n -> n.(fn n -> fn f -> fn x -> (
n.(fn g -> (fn h -> h.(g.(f)) end ) end).(fn y -> x end).(fn y -> y end) )
end end end).(m) end end).(m).(n)).(n).(x) end).(m)
end) end) end)).(n).(fn p -> (fn x -> p.(p.(p.(p.(p.(p.(p.(p.(p.(p.(x)))))))))) end) end))
end
end)).(n))
))
end)
end
## HELPERS
def to_c(c) do
Enum.slice(["0","1","2","3","4","5","6","7","8","9","B","F","i","u","z"], to_i(c), 1)
end
def to_s(p) do
Enum.join(Enum.map(to_array(p), fn x -> to_c(x) end))
end
def to_i(p) do
p.(fn n -> n + 1 end).(0)
end
def to_boolean(p) do
p.(true).(false)
end
def to_array(p) do
left = fn p -> p.(fn a -> fn b -> a end end) end
is_empty = left
array = []
do_to_array([], p, to_boolean(is_empty.(p)))
end
defp do_to_array(array, p, false) do
true_ = fn x -> (fn y -> x end) end
false_ = fn x -> (fn y -> y end) end
pair = fn x -> fn y -> fn f -> f.(x).(y) end end end # vireo
left = fn p -> p.(fn a -> fn b -> a end end) end
right = fn p -> p.(fn a -> fn b -> b end end) end
empty = pair.(true_).(true_)
unshift = fn l -> fn x -> pair.(false_).(pair.(x).(l)) end end
is_empty = left
first = fn l -> left.(right.(l)) end
rest = fn l -> right.(right.(l)) end
do_to_array(array ++ [first.(p)], rest.(p), to_boolean(is_empty.(rest.(p))))
end
defp do_to_array(array, p, true) do
array
end
def fizzbuzzdecoder(fb) do
Enum.map(to_array(fb), fn x -> to_s(x) end)
end
end
Enum.map(Church.to_array(Church.fizzbuzz), fn x -> IO.puts Church.to_s(x) end)
@emadb

This comment has been minimized.

Copy link
Owner Author

emadb commented Aug 8, 2019

The idea comes from here: https://codon.com/programming-with-nothing and this is a sort of port from Ruby code.
To execute the code simply download the file and run it from terminal with:

$ elixir fizz_buzz.exs

It will print the first 20 number of fizz buzz game (and some warnings :) )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.