Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: RFC: A Perlesque Introduction to Haskell, Part One (draft)

by runrig (Abbot)
on Jun 23, 2004 at 23:46 UTC ( #369205=note: print w/replies, xml ) Need Help??


in reply to Re: RFC: A Perlesque Introduction to Haskell, Part One (draft)
in thread RFC: A Perlesque Introduction to Haskell, Part One (DRAFT)

As for pattern-matching, the absolute coolest demonstration of this is quicksort in 2 or 3 lines of Haskell

Just for the fun of it, and for comparision, here is what (I think) the equivalent perl would be (for numeric sorts, I don't know if Haskell has a separate character comparision operator like Perl):

sub qs { return unless @_; my $x = shift; return qs(grep($_ < $x, @_)), $x, qs(grep($_ >= $x, @_)); }
It can be done without the $x temp variable and the shift (saving one line), but then it's less readable IMO.

Replies are listed 'Best First'.
Re^3: RFC: A Perlesque Introduction to Haskell, Part One (draft)
by Errto (Vicar) on Jun 24, 2004 at 01:29 UTC

    Of course, since Perl allows in-place modification of arrays, you can implement quicksort much more efficiently than that in Perl, but that way is much more cool-looking.

    Incidentally, there is only one set of comparison operators in Haskell. To deal with the fact that Haskell is a strictly-typed language, there are multiple implementations of the same operator for different argument types. This is done throught the type classes that FoxtrotUniform mentioned. The determination of which implementation to use in a given expression is made at compile-time, which is both very powerful and very annoying. Getting Haskell programs to compile is rather difficult, but once they do they stand a pretty good chance of working the first time.

Re^3: RFC: A Perlesque Introduction to Haskell, Part One (draft)
by ihb (Deacon) on Jun 24, 2004 at 02:28 UTC

    Just for the hell of it:

    sub qs { local ($x, @xs) = @_ and return qs(grep($_ < $x, @xs)), $x, q +s(grep($_ >= $x, @xs)) or () }

    ihb

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://369205]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2020-02-16 21:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (70 votes). Check out past polls.

    Notices?