Tag Archives: haskell

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:

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.