by Tarean · 7y ago · 0 comments
I always enjoy how lambda calculus suddenly becomes a readable language after the prelude:
let (\n.
let (\m. isZero (mod n m)) \divisibleBy.
if (and (divisibleBy 3) (divisibleBy 5))
then FizzBuzz
else
(if (divisibleBy 3)
then Fizz
else
(if (divisibleBy 5)
then Buzz
else (intToStr n))))
\fizzBuzzStep.
(loop \recurse. \n.
if (equals n 100)
then nil
else (cons (fizzBuzzStep n) (recurse (n+1)))
) 1)
(\letArg letBody. letBody letArg)
let, if, then, else, loop, and even the numbers are all functions.If someone can't do it at all there's some kind of serious issue with the way they're approaching the problem, or perhaps they're just not capable of doing it.
But another common way for people to fail or at least get red-flagged is to over-think it and come up with a turgid solution-- like using lambda calculus!
Simple problem. Simple solution. Nothing fancy. Going against that is asking for failure.
Keep in mind, too, that the lambda calculus is a lot like machine language in one respect - the same little bit of "code" can have more than one meaning to the programmer even if the "machine" is doing exactly the same thing.
(\id. ...) (\x. x)
This names the identity function id. You can make this slightly prettier like (\let. ...) (\def body. body def)
Which lets you do let (\x. x) \id. ...
Booleans are also functions let (\t f. t) \true.
let (\t f. f) \false.
let (\x. x) \then.
let (\x. x) \else.
let (\b thenLit ift elseLit iff. b ift iff) \if.
Which lets you write if true then foo else bar
Where then and else are thrown away, without them it is basically lisp syntax. You could make this slightly fancier - translate into cps to support `else if` and require `end` at the end.Tl;dr: with hacks