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.
mmm, seems to me that you have picked a rather boring language to learn. I am no for loops, seriously?
And do you have to previous every line in a function with the function name?
How about another crash course
Yeah, no loops at all. It’s like learning programming from scratch.
And, no, actually the three lines in the post are like different functions; you don’t need to start every line with the name. It’s really interesting when you get to it.
Random thought: Is Haskell pronounced like Pascal? 😀 (I don’t)
I need to learn a functional language too (ugh, I’ve been saying this for the better part of like two years now). Maybe JS. 😀
Yup, like Pascal. How do you pronounce? 😀
JS is a smart choice 😉 but you’d like Haskell. It’s a whole new world.