Created
October 16, 2019 14:04
-
-
Save vysogot/29b9dce8c68fffdb8246f5184fce8ee9 to your computer and use it in GitHub Desktop.
Ruby lambda factorial
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
IS_ZERO = -> (x) { x == 0 } | |
ONE = -> { 1 } | |
ONE_LESS = -> (x) { x - 1 } | |
ITSELF = -> (x) { x } | |
COND = -> (cond) { -> (doit) { -> (doelse) { cond ? doit.(nil) : doelse.(nil) } } } | |
MULT = -> (x) { -> (y) { x * y } } | |
puts( | |
-> myself { | |
-> (n) { | |
COND.( | |
IS_ZERO.(n) | |
).( | |
-> _ { ONE.() } | |
).( | |
-> _ { MULT.(ITSELF.(n)).(myself.(myself).(ONE_LESS.(n))) } | |
) | |
} | |
}.( | |
-> myself { | |
-> (n) { | |
COND.( | |
IS_ZERO.(n) | |
).( | |
-> _ { ONE.() } | |
).( | |
-> _ { MULT.(ITSELF.(n)).(myself.(myself).(ONE_LESS.(n))) } | |
) | |
} | |
} | |
).(6) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment