Diving into Haskell

As mentioned in the previous post, I started a ‘Learn Haskell in a Day’ project yesterday evening. As of this writing, I’ve finished the 5th of the 14 chapters in the tutorial.

Yeah, it’s a bit disappointing. Well, my uncle and his daughter came to stay yesternight, so I couldn’t focus on Haskell while they were here, could I? Could have made a better excuse out of that, but to whom am I cheating?

Even the tutorial I’m following is quite

Anyway, here are some interesting stuff I came across after yesterday’s post:

Types and Typefaces
Types are much like the types of variables you can use. Those include Int, Integer (which is an unbounded int), Char, Bool, Float, etc etc. A string is a list of Chars. Talking about lists, they play a central role in Haskell. Much more powerful than the lists in the languages we are used to.

Typefaces are sort of like interfaces in OOP. Types implement typefaces. (At least that’s what happens as far as my current understanding goes). A type that implements the Eq typeface can be checked for equality. One that implements Show can be presented as a string. For example, you can make a function that takes types that implements both Eq and Show.

No loops for you, baby!
Haskell has no for or while loops. Like, seriously. But you can use recursion elegantly to achieve what you’d have done with loops in an imperative language. This can be tricky at first; how does one do _anything_ without a loop?

Suppose we want to write a function to get the maximum value in a list. Of course there’s an in-built function, but say we wanted our own. How we do it is create a recursive function that would return the maximum of the current element and the maximum of the rest of the list, which, as you can see, is a recursive call.

maximum' :: (Ord a) => [a] -> a
maximum' [] = error "maximum of empty list"
maximum' [x] = x
maximum' (x:xs) = max x (maximum' xs)

This uses the in-built max function to get the maximum of two values.

Tabs are evil
The GHC compiler does not get on well with tabs. Spent nearly 20 minutes one time to find a bug which had been caused by using tabs. The solution is to ask your editor to use spaces for tabs. If you use Vim, this can be done using :set expandtab.

I tried implementing stuff like the fibonacci series and some math problems, which, IMO, is one of the best ways to learn a language. There’s always a better implementation with more elegant code, though.

So.. I’m hoping to finish at least 3 more chapters by 6pm today. Guess it’d be as easy as it sounds.

Also, funny story, the internet connection here sucks so much that I wasn’t able to clone a 6MB repo from BitBucket. And I tried 6 times. Yeah, not easy to imagine.

Await another update later today.

Learn Haskell in a Day

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:

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,

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.

Quantum particles are like boos

If there’s one single subreddit that’s worth subscribing to, it’s /r/explainlikeimfive. The subreddit aims at answering to questions like you’re explaining to a 5 year old, i.e. in a language that everyone can understand.

When I come across a good explanation I clip them to my evernote. I was going through some notes today and came across this clip from a post that dated about two months back. The post explains the work of 2012 Nobel prize winners. There is an explanation that says quantum particles behave differently when we observe them. Someone raises the reasonable question “How do they know they are doing something different when we don’t observe them if we can’t observe them without affecting them previously?” Look at the explanation it got (permalink):

super mario boos

It’s like the boos in Mario. Whenever you look at (measure) them, they stop moving (act like particles). When you look away, they chase you (act like waves). When you look at them again, they’re somewhere totally different.

And just like boos, sometimes they go through walls. But only when you’re not looking.

Exactly what a 5-year would need to understand what’s going on. What’s more, it explains a scientific principle so brilliantly that any of us can understand (albeit it may not be 100% technically correct).

I highly recommend reading the whole thread.