I'm finally making an effort to actually learn
Haskell.
Brief dabbling, general buzz, and personal recommendations all point to it being awesome
and flat out making you a better programmer.
I don't think I'll be following John's method of
learning Haskell by writing a compiler, but
I am following this
tutorial
on writing a Scheme
interpreter per his recommendation.
Made this snowflake fractal as part of working through (Amazon.com) The Haskell School of Expression: Learning Functional Programming through Multimedia
Haskell: w(hy)tf?
Language wars are silly, and I don't pretend to know enough about Haskell to say that one _should_ develop in it. However, learning a language that admits (or beter yet, requires) concepts that are new to you is unlikely to make you a worse programmer/thinker/person. For a nice treatment of this, see The Perils of JavaSchools.
So, what are these motivating concepts for Haskell? As stated up top, I'm busying myself finding out.
The fact that it is purely functional
and lazy is really exciting enough for now,
despite not getting past the first 3 lines of the wikipedia page. I'm also kind of psyched to dip into
Category theory through
monads, which will allegedly
break my brain.
What does Haskell look like?
Compellingly like math, (if math compells you). Check out this 6-line quicksort (line numbers added for clarity):
2) quicksort [] = []
3) quicksort (x:xs) =
4) quicksort [y | y <- xs, y<x ]
5) ++ [x]
6) ++ quicksort [y | y <- xs, y>=x]
This well-known algorithm showcases the power of list comprehension, which reads very much like set notation. Line-by-line, this code can be read as follows:
- 1) quicksort is a function from a list of some type to another list of that same type, given that the type is orderable.
- 2) quicksort of an empty list returns an empty list.
- 3) quicksort of a list which we split into the first element, 'x', and the rest 'xs' is
- 4) The list returned by quicksorting a list made of elements 'y' such that each 'y' comes from the list 'xs' and is less than 'x'
- 5) Concatenated with a list containing just 'x'
- 6) Concatenated with the list returned by quicksorting a list made of elements 'y' such that each 'y' comes from the list 'xs' and is greater than or equal to 'x'.
Note that, while brief, this is a very readable way to do quicksort in Haskell. Line 1 is optional, and lines 3-6 are separated for readability.
Lazy Functions
Functions in Haskell are lazy. That is, they evaluate only as much as they need to. This allows for, amongst other things, infinite data structures. Check out this list of all positive integer factorials:
factorials = 1 : zipWith (*) [2..] factorials
The top line just states that 'factorials' is a function returning a list of integers. 'zipWith' takes a binary function and two lists, returning a list formed by applying the function to the elements of the two lists. Check out the code for zipWith in the Haskell Prelude.
Thus the above code defines 'factorials' recursively as '1' prepended
(with the operator ':') onto the list formed by applying multiplication in turn to the elements
of the integers greater than 2, and the list 'factorials' itself.
That is we prepend 1 onto the list
[2 * (factorials[1] = 1), 3 * (factorials[2] which is the previous element, 2), ...]
Thanks to lazy evaluation, this can then be used like ' take 200 factorials' without concern for actually generating any factorials other than the first 200.
Haskell Links
The Haskell homepage
A Tour of the Haskell Prelude
A Gentle Introduction to Haskell
John's Haskell reference links.
Learning Haskell
Caltech's CS11 Haskell Track
(Amazon.com) The Haskell School of Expression: Learning Functional Programming through Multimedia