List Info

Thread: Re: lambda, ->, haskell, and so on




Re: lambda, ->, haskell, and so on
user name
2008-05-25 08:20:50
As background: I've been doing Ruby full-time for about four
years,
until about two months ago, when I started doing Haskell
full-time
(though I still do a day or so a week of Ruby).

On 2008-05-09 00:33 +0900 (Fri), ara.t.howard wrote:

> well, as michael pointed out, it's already there an i
that ruby has shown 
> itself to be moving more and more into the functional
realm....

Not really so much, no.

> ...but feel that the functional languages should take a
page from ruby
> not the other way around - ruby's syntax is easily
among the prettiest
> on any existing multi-purpose language.

I'd disagree.

Ruby is somewhat pretty, but it's got big consistency
problems,
especially compared to, say, Smalltalk, Haskell, or
whatever.

And changing Haskell to be more like Ruby, as one example,
would make a
lot of typical FP techniques more difficult.

This all has little bearing on the arrow syntax, however. It
seems to
make little difference (other than asthetic and in terms of
generating
a parser) in this situation, and it's not even all *that*
terribly
common in Haskell anyway, where programmers tend to be far
more reliant
on building new functions with combinators and passing them
around as
first-class values.

As a simple example of Haskell's expressive power, let's
look at a
polymorphic (but statically type checked) function to sort
pairs of
things, but with the order of the second part of the pair
taking
precedence over the first:

    sortSndFst [(1,5),(3,3),(2,3),(0,5),(5,0)]
    -- result: [(5,0),(2,3),(3,3),(0,5),(1,5)]

You could define it like this:

    sortSndFst list =
        sortBy (a b -> case compare (snd a) (snd b) of
                             LT -> LT
                             GT -> GT
                             EQ -> compare (fst a) (fst
b))
	       list

but a much more functional style is a quarter the size (I've
added extra
parens for evaluation order clarity, though they could all
be replaced
by a single '$' after 'sortBy':

    sortSndFst = sortBy ((comparing snd) `mappend`
(comparing fst))

This makes use of currying, passing functions into other
functions to
generate new functions, a Monoid for control flow, and it's
small enough
to be so obviously correct (if it passes the typechecker)
that I don't
even need to bother with a type declaration (it's inferred)
much less a
unit test. (Oh, and not one lambda. )

Much of this stuff can also be done in Ruby, but not in
anywhere near
as clean or reliable a way, though I'd be very pleased if
Ruby moved in
that direction.

cjs
-- 
Curt Sampson       <cjsstarling-software.com>  
     +81 90 7737 2974   
Mobile sites and software consulting: http://www.starling-
software.com


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )