ser / germane-software.com (Sean Russell) writes:

> James Britt <jamesUNDERBARb / neurogami.com> 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.

Lots of functional languages lack pattern matching.  Scheme, for
example.  One can write Scheme-styled code in Ruby.

> 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.

I disagree.  Ruby lacks purely functional I/O, but so do Scheme and
Lisp.  I don't see why Scheme would surpass Ruby's functional
programming ability.

> [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.

In Scheme, sum would be:

 (define (sum xs)
   (if (null? xs)
      0
     (+ (car xs) (sum (cdr xs)))))

Translating to Ruby, we get:

 class Array
   def sum (xs)
     if xs.empty? then
       0
     else
       xs.first + xs.tail.sum
     end
   end

   def tail
     self[1..-1]
   end
 end

You're mistaken in believing that functional programs must lack method
calls.  Scheme's CAR and CDR are just as `object-oriented' as Ruby's
first and tail.  Ruby just makes it explicit.

  mikael