Last active
December 30, 2016 13:48
-
-
Save dmmfll/bfa6fe20cf9ccf072784387945c4695e to your computer and use it in GitHub Desktop.
This file contains 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
# print an return, print is side effect | |
identity_print = lambda { |x| | |
puts x | |
x | |
} | |
word = lambda { | |
# return a random word of minimum length 3 | |
(3..(7..20).to_a.sample).to_a.sample.times.map { | |
('a'..'z').to_a.sample }.join | |
} | |
_quit = lambda { # quit is a keyword in Ruby?! | |
# return the string 'quit' | |
'quit' | |
} | |
# make a list of random strings and insert 'quit' randomly | |
random_words = lambda { | |
stop = (5..20).to_a.sample | |
samples = (3..stop).to_a.sample.times.map { | |
word[] | |
} | |
# samples.compact | |
samples.insert (1..samples.length).to_a.sample, _quit[] | |
} | |
# return a random word, emulating user input | |
_input = lambda { | |
random_words[].sample | |
} | |
# recursion | |
echo_FP = lambda { | |
identity_print[_input[]] == _quit[] or echo_FP[] | |
} | |
echo_FP[] |
This file contains 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
# FP version of "echo()" | |
def identity_print(x): # "identity with side-effect" | |
print(x) | |
return x | |
echo_FP = lambda: identity_print(input("FP -- "))=='quit' or echo_FP() | |
echo_FP() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment