I was intrigued by Raditha‘s posts on learning Ruby and Python in a day (though they were dragged to 48 hours). Now I’m going to make that inspiration into action by trying to learn Haskell in a day.
Why Haskell?
I’ve always wanted to learn a functional programming language. Have never even written a Hello World with one. Heck, I even don’t know what functional programming is. My first choice was Clojure, but then, after spending some time on the web, decided it would be Haskell. So the main reasons were:
1. Haskell is purely functional
2. There’s a strong, near-fanatical community. Smaller ones are frustrating to work with (that’s what she said).
3. I had heard of ppl solving problems with Haskell in numerous forums
4. When asked what languages I’m proficient in, it’s always nice to add “and, of course, Haskell” at the end.
As the tutorial, I chose LYAH (Learn You A Haskell), which is the ‘funkiest way to learn Haskell’. It’s aimed at those who already know how to programme, so, yeah.
And this will be just a crash course. I very well know you can’t become a master in a language within 24 hours. What I’m trying to achieve is being able to write code to solve basic problems and being able to read Haskell code when I see some.
And here we go:
Language: Haskell
Compiler: GHC (This gets installed when you install the haskell-platform)
Editor: Vim
Tutorial: LearnYouAHaskell
Motivation level: High
Was going to start in the morning, but started just an hour ago coz of the darn slow connection which is a luxury in this wilderness. It took more than half-an-hour to download the 70MB of packages for haskell-platform, just imagine. Hoping to finish by this time tomorrow if the connection keeps up till then.
As of this writing, I’m in the section “I’m a list comprehension”. Here are a few interesting stuff I came across so far:
Functions
As you may have guessed, functional programming languages are all about, well, functions. So is Haskell. Declaring a function is as easy as declaring a variable.
squareMe x = x * x
Functions and parameters are separated by spaces and not parentheses, just like in Bash. Also, if the function takes exactly two parameters, you can call it as if it were an infix function. For example, the ‘elem’ function takes two parameters. If we wanted to pass 2 and someVar to it, both of the following will work:
elem 2 someVar
2 `elem` someVar
Using infix functions can be less confusing in many circumstances.
Error messages
Ugh. The error messages Haskell gives are almost incomprehensible. If you tried to add a string to an integer it would be,
<interactive>:39:3:
No instance for (Num Char)
arising from a use of `+’
Possible fix: add an instance declaration for (Num Char)
In the expression: 2 + ‘c’
In an equation for `it’: it = 2 + ‘c’
An if statement is an expression
You can do cool stuff like this:
coolFunc x = (if x > 10 then x else x*2) + 1
So if we passed 4 to coolFunc
it would return 9 (= 4*2 + 1) and if the argument happened to be 15 it would return 16 (= 15 + 1).
Haskell is lazy
Haskell is a ‘lazy‘ programming language. By lazy, it means that it won’t compute anything until its result is specifically asked for. For example, we can use the following to obtain the first 20 multiples of 7:
take 20 [7,14..]
[7,14..]
denotes the infinite array 7,14,21,28 and so on. The above statement asks for the first 20 elements of this infinite array. In another language, the interpreter would try to calculate the infinite array first before taking the first 20. But since Haskell’s lazy, it doesn’t do anything until the function ‘take
‘ asks for the first 20 elements. It will only calculate the first 20 elements.
For a moment I thought functional languages are like procedural languages and was almost going “kids these days” 🙂
Question though, do functional languages offer anything above the “other” (I think “imperative” is the name) languages?
As I’ve heard, they support concurrency more than any other, hence such languages are preferred for mathematical problems that are computationally intensive. I’m yet to see why this is so, though.
Also, they can solve problems in one or two lines of code which take dozens in others.
All the best!
I have already forgotten all the Ruby I learnt in that project but not so python.
Thanks! Perhaps a little time in Rosetta Code with that language each week would help?