From: bbense+comp.lang.ruby.Aug.23.02@... Date: 2002-08-24T07:59:33+09:00 Subject: Re: A Repeat: New Language After Ruby? -----BEGIN PGP SIGNED MESSAGE----- In article , William Djaja Tjokroaminata wrote: >Hi, > >Thanks for sharing your thoughts. Is it really true that Ruby is in many >ways a functional language? I know that Ruby was influenced to a certain >degree by Lisp, but can you elaborate on this further? If Ruby indeed has >some functional language nature, I will nominate Ruby as "the ultimate >functional language" :), just like one C course instructor called C as the >"ultimate assembly language". > - - The functional language purists would probably shoot me for saying this but while Ruby is not strictly a functional language, many of the idioms of functional language can be mapped quite easily to Ruby. For example: One of the classical papers on Functional Programming is "Why Functional Programming Matters" which you can get at http://www.md.chalmers.se/~rjmh/Papers/whyfp.html - - Let's take his first example. He shows how you can generalize a sum of list elements function to perform pretty much any reduction on a list. He defines reduce as (reduce f x ) nil = x (reduce f x ) (cons a l ) = f a ((reduce f x ) l ) The Enumerable module allows you to do very similar things to any object that supports "collect". def reduce ( proc, value ) result = value self.collect { |obj| proc.call(obj,result) } return result end While strictly speaking this is not functional programming, to me it "feels" like functional programming and the idea is certainly the same. i.e. rather than writing a specialized "sum" function for every list type, you write a function that knows how to traverse a list and one that knows how to "sum" two things together. Thus when you have a new data type or a new sum you only need to write a new function for "summing" two things together, you don't need to rewrite the code for traversing a list. - - Whenever I see something like phrase = "Now here's a sentence" phrase.split(" ").reverse.join.(" ") I think "functional programming". Perhaps, I just don't fully understand functional programming, but to me there's nothing particularly OO about the above code. - - The paper quoted talks about two key features of functional programming, Higher order functions and lazy evaluation. I can think of lot's of examples of the first in Ruby, but nothing for the second. I guess you could adapt the coroutine stuff to implement lazy evaluation, in fact that's the only real use I can think of for coroutines. - - Booker C. Bense -----BEGIN PGP SIGNATURE----- Version: 2.6.2 iQCVAwUBPWa6vmTWTAjn5N/lAQFFtgP5AW3ZBDwTVLWQW15ECiy6ZW1hADtP635Z hQROIyUogkYHS17n/NIL1mtHbeGWAuTVHLeaYci8kqM0T0UL/RQcTl2FDSFvoxIr VkmDjrMHp4k/q3KCjVddF2TVi347S3QMJc9v99Ik6n7ywa0ynBEty2zkYdAYhFcV RiVMFLBFy/U= =EnCF -----END PGP SIGNATURE-----