Getting started with Haskell… still
I can’t help but think there’s a bit of a gap in the market for introductory texts on Haskell. I say this in part because, at time of writing, if you google (hah! I’m using it as a verb! Trademark that!) “Getting Started Haskell”, you might end up here =/
I’d resolved to try getting started again on account of continuing to hear people rave about the language, so last night I did what I always do when learning something new – I googled “Getting Started X”. I found this awesome e-book / blog:
It’s very well written (if a little rough round the edges – beta is the word), but I still think the learning curve presented is a _little_ steep for simpletons like myself. Bear with me while I expose my ignorance.
We’re using GHC. GHC is recommended by the book, it’s recommended by Don (who is indescribably leet: http://cgi.cse.unsw.edu.au/~dons/blog/), and it’s recommended by my n-sim colleagues (who mostly are, except for me: http://www.n-sim.com).
# apt-get install ghc6 # ghci GHCi, version 6.8.2: http://www.haskell.org/ghc/ : ? for help Loading package base ... linking ... done. Prelude>
aaah. Prelude. Don’t I feel at home. In fact I don’t – this is all pretty weird, but working through the first couple of chapters of the book was fun. Walk with me a while…
If types don’t excite you, this probably isn’t the language for you. But they should; they’re awesome.
Prelude> :set +t Prelude> 1337 1337 it :: Integer
In GHCI (interactive GHC interpreter), setting “+t” makes the interpreter print the type of whatever you’ve just evaluated. Technically, it prints the type of “it” – the special value in to which your last evaluated expression is loaded (there is no spoon, there are no variables). I know what “1337″ is, I know what “it” is, but what’s “Integer”?
Prelude> :info Integer data Integer = GHC.Num.S# GHC.Prim.Int# | GHC.Num.J# GHC.Prim.Int# GHC.Prim.ByteArray# -- Defined in GHC.Num instance Enum Integer -- Defined in GHC.Num instance Eq Integer -- Defined in GHC.Num instance Integral Integer -- Defined in GHC.Real instance Num Integer -- Defined in GHC.Num instance Ord Integer -- Defined in GHC.Num instance Read Integer -- Defined in GHC.Read instance Real Integer -- Defined in GHC.Real instance Show Integer -- Defined in GHC.Num
ooo… fancy. What does that all mean? Well, I’m only on chapter 3, but in my simplistic Object Oriented view of the world, we’re effectively saying that Integer implements the interfaces Enum, Eq, Integral, Num, Org, Read, Real and Show (but the truth is a little more involved).
Prelude> :info Enum class Enum a where succ :: a -> a pred :: a -> a toEnum :: Int -> a fromEnum :: a -> Int enumFrom :: a -> [a] enumFromThen :: a -> a -> [a] enumFromTo :: a -> a -> [a] enumFromThenTo :: a -> a -> a -> [a] -- Defined in GHC.Enum instance Enum Integer -- Defined in GHC.Num instance Enum Float -- Defined in GHC.Float instance Enum Double -- Defined in GHC.Float instance Enum Bool -- Defined in GHC.Enum instance Enum Ordering -- Defined in GHC.Enum instance Enum Char -- Defined in GHC.Enum instance Enum () -- Defined in GHC.Enum instance Enum Int -- Defined in GHC.Enum
You kind of have to be comfortable with looking at types of the form:
a -> a -> a -> [a]
“enumFromThenTo” obviously takes three values and returns a list of values. (The joy of types, right? You know what it does by what its type is.) Moreover, we can see that it’s defined for the instances Integer, Float, Double, Bool, Ordering, Char, () and Int.
Prelude> enumFromThenTo 1 2 8 [1,2,3,4,5,6,7,8] Prelude> enumFromThenTo () () () [(),(),(),(),(),()^CInterrupted.
and we can type functions too:
Prelude> :type fst fst :: (a, b) -> a
What does that one do? There's only one thing it can do! Yes, I know, that's practically pornographic.
"Write a function lastButOne, that returns the element before the last."
Trivial, right? A five year old could do it. Well, excuse me while I have a quick flashback to ML ticks (PDF) and rock gently back and forth in the corner. You have to bear in mind that, at this point in the book, we don't know there's a 'length' function in Prelude, we've not been taught pattern matching or case statements, and we're still simplistically minded imperative programmers. So naïvely, the best we might be able to do is:
-- in add.hs count n  = n count n xs = count (n+1) (drop 1 xs) myLastButOne xs = head (drop ((count 0 xs) - 2) xs) Prelude> :load add.hs [1 of 1] Compiling Main ( add.hs, interpreted ) Ok, modules loaded: Main. *Main> myLastButOne [1,2..10] 9
Repeat after me… “ewww”. And even to do that, I’ve had to use mysterious pattern matching which hasn’t been explained at that point in the book. Now, we could assume that a resourceful reader might find the ‘length’ function:
myLastButOne xs = head (drop ((length xs) - 2) xs)
But that’s still pretty unsatisfactory. For all I know, length is O(n) in the length of the list, so I’ll be going down the list twice. I daren’t imagine what Don would say. Even if it’s O(1), it doesn’t feel right. After a bit of head scratching and syntax guessing, I came to:
lastButOne (h:t) = case t of (a:) -> h (a:b) -> lastButOne t
which, for me at least, feels a little better. But I don’t know it’s right. I’m welcoming any pointers here. Now obviously for the “Find the last but nth item”, going down the list twice is looking less unattractive:
myLastButN n xs = head (drop ((length xs) - (n+1)) xs)
It’s still not great though. Would that I could list[-n]. But that’s not the point.
I’m determined to learn more Haskell and continue to expose my ignorance on this blog. Any pointers to good docs are welcome – “Haskell for simpletons”, that sort of thing. Meantimes I’ll continue to read the book. My stretch goal is to understand the things written on Don’s blog and on the following: