|
|
So I haven't visited Haskell.org for a while - mainly because it updates
so rarely. But it appears to have had a major facelift.
Imagine my horror on seeing that the example program both
incomprehensible and considered a Haskell anti-pattern! o_O
Still, they added a REPL directly to the front-page, which is nice. I
can remember requesting this for ages. Well, it seems it's there now.
Neato. (They make an offline tool that did this, then added the feature
to an IRC bot, then made a 3rd-party website that did it... and now it's
finally on the official site!)
It seems the old front-page is still there. And still hasn't been
updated this year. But hey, at least you can see the "downloads" link
directly from the new front-page. The "news" links to Twitter, Reddit,
StackOverflow, etc. Not sure that's optimal; you get a screenful of
social media "noise", rather than a "here are the major events that
happened recently" written by a human. Still, I guess it updates more
often! :-P
The example they chose to showcase is (and I quote):
primes = sieve [2..]
where sieve (p:xs) =
p : sieve [x | x <- xs, x `mod` p /= 0]
This is not exactly the most comprehensible Haskell fragment ever. And
it's even formatted strangely. A normal human would probably write
primes = sieve [2..]
where
sieve (p:xs) = p : sieve [ x | x <- xs, x `mod` p /= 0 ]
Even then, it's just not a particularly good example. It's on the same
level as the infamous
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
Until you understand quite a bit about Haskell, this is utterly
incomprehensible. (This example, at least, has the potential for being
quite efficient, offering O(N) access to the Nth Fibonacci number.)
A better example - and one which was used in the past - is quicksort:
quicksort [] = []
quicksort [x] = [x]
quicksort (x:xs) =
let
ys1 = quicksort $ filter (<= x) $ xs
ys2 = quicksort $ filter (> x) $ xs
in ys1 ++ [x] ++ ys2
If you know how quicksort works, you can take a stab at what the hell
this is doing, which is a start. (There's still plenty of strange syntax
to trip you over. For example, WTF does ":" do??) It does demonstrate
several neat Haskell features. For example, you define quicksort by
saying "if the input looks like XXX, produce output YYY" several times.
You can filter a list on a condition as trivially as "filter (> x)
list". And so on.
Of course, quicksort only looks succinct because the filter function
just happens to be predefined. If you try to do a mergesort... well,
there's no predefined merge function. There's also no predefined way to
split a list in half. In fact, it's not immediately obvious how to do
that with a linked list. Assuming you don't need a stable sort, you can
just "split" by taking even/odd list elements, like so:
even_list ( []) = []
even_list (x:xs) = x : odd_list xs
odd_list ( []) = []
odd_list (x:xs) = even_list xs
merge ( []) ( ys) = ys
merge ( xs) ( []) = xs
merge (x:xs) (y:ys) =
if x < y
then x : merge ( xs) (y:ys)
else y : merge (x:xs) ( ys)
mergesort [] = []
mergesort [x] = [x]
mergesort xs =
let
ys1 = mergesort ( odd_list xs)
ys2 = mergesort (even_list xs)
in merge ys1 ys2
There is of course a whole bunch of subtlety going on here. If you don't
understand pattern-matching properly, it's probably quite hard to follow
what's going on. (Especially since list pattern syntax is so odd.)
Still, it could be worse. The example could have been
main = putStrLn "Hello World!"
which would tell you *nothing* about what makes Haskell different from
the two-dozen scripting languages of the world where Hello World is
similarly terse...
Post a reply to this message
|
|