Using redo for tail recursion in Ruby is an interesting idea, similar to Clojure's recur keyword.
but ruby doesn't support tail recursion effectively? or is this something new in 1.9. Stack level is not deep enough in 1.8 :P
Ruby’s Most Underused Keyword
11–13 of 13 posts
Re: Ruby’s Most Underused Keyword
#12redo, in the Fibonacci example, is still iterative - it's presented as a third way, when it's just a cruftier way of being iterative. If you remove the cruft of the lambda and the redo, you end up with: def fib(i) n, result = 1, 0 while i != -1 i, n, result = i - 1, n + result, n end result end Cleaner, faster (by 10% on Ruby 1.8) and shorter, but certainly iterative. (Separate to this, the fib routines shown produce…
You are absolutely right... if you're an interpreter... From a user's point of view, the redo says "run this block again"; and since the block == the method it's just another way to run acc(). Of course, it's still faking , but I've never claimed anything else :-)
"run this block again" signifies "loop" to me and "loop" signifies "iteration." If I see a block of code being looped, I don't think (as a coder, not as an interpreter) that it's recursive. Perhaps others would, though. If we got in a time machine back to the 80s, I kinda feel that "redo" would turn into "goto".. :)
(Hey, we should be doing this on RubyFlow as well - you could be earning some serious karma :))
Re: Ruby’s Most Underused Keyword
#13redo, in the Fibonacci example, is still iterative - it's presented as a third way, when it's just a cruftier way of being iterative. If you remove the cruft of the lambda and the redo, you end up with: def fib(i) n, result = 1, 0 while i != -1 i, n, result = i - 1, n + result, n end result end Cleaner, faster (by 10% on Ruby 1.8) and shorter, but certainly iterative. (Separate to this, the fib routines shown produce…