From: ser@... (Sean Russell) Date: 2004-07-07T00:17:44+09:00 Subject: Re: Functional Ruby (Re: Why I don't use Ruby.) James Britt wrote in message news:<40E586A6.9040301@neurogami.com>... > Um, I don't see this as true. Certainly one can write all the > procedural code one likes in Ruby (I know *I've* managed to do it), and > I believe the same is pretty much true for writing functional code. "Functional code" is a rather broad term, but one popular feature of many functional languages is pattern matching[1], which Ruby doesn't support. Indeed, the entire structure of functional programs is generally alien enough to OO or procedural languages that any mapping you could do would be difficult to describe as being functional, except in the broadest sense. Procedural and OO languages are very similar. Functional languages require a different mindset, and I believe that it *is* a stretch to claim that you can do functional programming in Ruby. Furthermore, I'd say it would be difficult to do pure procedural programming in Ruby -- you'd have to redefine most of the API to avoid calling methods on objects -- however, it could be done. [1] in the sense of matching the appropriate function based on the arguments. Consider the function sum(). Given a recursive procedural definition: int sum( array ) { if ( length( array ) == 0) return 0; else return array[0] + sum( subset( array, 1, length( array ) - 1 ) ); } In Ruby(procedural) this might be: sum( array ) { return 0 if length( array ) == 0 return array[0] + sum( subset( array, 1, -1 ) ) } (being pedantic, and avoiding *all* OO methods). In Haskell, this code would be: func [] = 0 func (x:y) = x + func( y ) Converting the Haskell code to Ruby would give you the same code as the procedural version (again, if you avoid all of the OO constructions that you possibly can). I don't think I'd be exaggerating if I said that, in general, Ruby( procedural ) == Ruby( functional ), and yet, functonal and procedural programming are fundamentally different styles. --- SER