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, @_));
}
[download]```
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 () }
[download]```

ihb