From: Mikael Brockman Date: 2004-07-07T03:49:51+09:00 Subject: Re: Functional Ruby (Re: Why I don't use Ruby.) Hal Fulton writes: > Martin DeMello wrote: >> Joel VanderWerf wrote: >> >>> Ruby doesn't optimize this tail recursion, but Scheme does. That's >>> one way Scheme could be said to be more "functional". >> http://www.ccs.neu.edu/home/matthias/Presentations/ecoop2004.pdf >> suggests that no language should be called properly OO unless it does >> tail call optimisation :) > > Heh. I hereby pronounce that no language is truly OO unless its name > starts with an R and ends with a Y. A noble initiative, but Felleisen's is slightly less arbitrary. He shows how a properly OO linked list needs TCO. module ProperlyOO class Cons def initialize (car, cdr) @car, @cdr = car, cdr end def length # This requires tail-call optimization. 1 + cdr.length end end class Empty def length 0 end end end module ImproperlyOO class Cons def initialize (car, cdr) @car, @cdr = car, cdr end def length # This doesn't require TCO, but it's ugly. length = 0 current = self until current.kind_of? Empty length += 1 end length end end class Empty def length 0 end end end I guess the iterative version would be better with a method is_empty?, but I'm just the messenger. mikael