If any of you are masochists and want to turn your brains to mush thinking about blocks, here is a challenge for you:
Given this code, do the following steps in order. It is strongly recommended that you do not read ahead (ignorance is bliss)
countdown = lambda do |n, recurse|
n # => 5, 4, 3, 2, 1, 0
break if n <= 0
n # => 5, 4, 3, 2, 1
puts n
recurse.call(n-1)
end
enable_recursion.call(countdown).call(5)
# >> 5
# >> 4
# >> 3
# >> 2
# >> 1
- Dim the lights and soften the music, this might give ryou a migraine so make the environment hospitable.
- Define
enable_recursion
such that this code does what it shows - Didn't I recommend not reading ahead? Oh... You got this far? O.o I admit, I am impressed. But here's the thing, your code is only allowed to use local variables and lambdas. No methods (other thanuse
.call()
on a lambda), constants, instance variables, objects, etc. Go fix that. - Alright, alright, 3 was your reprieve, get ready to spend the night huddled in a corner, nursing your brain and murmuring to yourself. Maybe meditate for a bit, make peace with an old enemy, write your will, that sort of thing.
- Hug someone you love! Other than
enable_recursion
, your code is not allowed to create local variables through assignment statements (a = 1
... not allowed) - Holy shit, you got through 5? If you need to talk to someone, don't hesitate to call me, okay? Seriously, we're here for you. But alas, the onslaught continues: fix my code, too. No code in your editor window may assign to a local variable (e.g. my
countdown
, yourenable_recursion
- Final stretch, lets turn it up to 11! Every lambda in all of this code must take exactly 1 argument.
- Alright, lets be honest, you're reading ahead. ...but if not, send me your work and I'll tell you what you did...