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

> Mikael Brockman <phubuh / phubuh.org> wrote in message
> news:<874qoee9pl.fsf / phubuh.org>...
>> I agree that it's difficult to program functionally in Ruby according
>> to those definitions.  I also think it's difficult to do so with
>> Scheme.
>
> There is quite a debate about how much of a functional language Scheme
> is.  I've heard it referred to as "relatively functional", and heard
> it said that Scheme "encourages" functional programming.  This isn't a
> trivial point; there are serious reasons for enforcing purity in
> functional programming, unlike the common reasons for enforcing purity
> in OO languages which often are based on aesthetic reasons.  Purely
> functional are verifiable and thread safe, which can be important.
>
> However, I know that some Schemers get touchy about this point, so I
> don't want to get into an argument about it.  Suffice to say that
> there's enough of a debate among people smarter than I about the
> purity of Scheme to give that opinion some validity.

I agree.  Haskell makes it easier to write purely functional programs.
Still, it's definitely possible to write mostly functional programs in
Scheme.  I think it's possible to write mostly functional programs in
Ruby, too, though probably to a lesser degree.

>> Nope, I haven't.  I try to follow the grain of the language as much as
>> possible, so with Ruby, I tend to write imperative object-oriented
>> programs.  Of course, many habits from my functional experience follow
>> me: defining new control structures, using heavy abstraction, etc.
>
> In my experience, many of my programs in Ruby start out being
> functional.  Just a line or two return results per line of a file. 
> Then they grow to be imperative, and I'm defining functions.  The
> final stage is when they metamorphose into true OO programs; I wrap up
> the functions in reusable Objects, and wrap the application up into a
> single (or a very few) instantiations and method invocations.  This is
> one of the reasons why I like Ruby so much.  Even though it is more OO
> than Java, Java is more of a pain because you have to start out in the
> OO paradigm, even when you don't need it.
>
> Real functional programming is also nice, though, and encourages you
> to think about the data you're working with.  I worked on one
> data-driven project where the "specification" ended up looking like a
> bunch of Haskell function declarations, so that's what I chose to
> write the application in.  It was, probably, the most robust
> application I've ever written.

I've had some love affairs with Haskell, too.  I wrote a client for a
Napster-like file sharing service called Direct Connect, which certainly
isn't Haskell's target market, and it was beautiful.  Algebraically
manipulating I/O actions is really pleasant, and monadic parser
combinators (Parsec, in my case) were awesome for parsing the wire
protocol.  That parser was probably the most robust parser I've ever
written.

One of the most memorable things was writing this Scheme macro:

| (define-macro (loop action)
|  (letrec ((f (lambda ()
|                (action)
|                (f))))
|    (f)))

as this Haskell:

| loop action = action >> loop action

and algebraically simplifying it to simply

| loop = fix (>>)

Another beautiful thing was this:

| downloadFile socket size fd = write fd (read socket size)

Which, thanks to lazy evaluation, intersperses the reads and the
writes.  If I wanted it to report its progress to, say, a progress bar,
I could just insert another function in the middle of the pipeline.

Damn you, Sean.  You got me in the mood for writing Haskell.

>> That's only true if you think of the data and its operations as
>> completely separate entities.  Doing so isn't easily justifiable in
>> Ruby, since they're defined in their own little indented scope.
>
> Right, but I think that's the point in a purely functional language.

Yup.  That doesn't mean I have to obey. :-)

Thanks for a nice discussion, Sean.

  mikael