It is Sunday evening – yesterday was a LAN and I was up gaming until the wee hours. Now I am back home. I have had enough of games for now; I am sleepy, but it is too early for bed; I feel like doing something. Why not Haskell?
I will begin the journey of learning described in learnhaskell, a guide by Chris Allen.
Installation
This was pretty straightforward. The steps I followed are here for posterity:
-
downloaded and installed Haskell Platform for Windows
- there was one little WTF in the process – after the installation was done, the wizard informed me that I did not have something called “GLUT”, and asked if I wanted to install it. No indication of what GLUT is. A little investigation indicated that it is probably something related to OpenGL. I decided to install it; I doubt I will use it any time soon, but I was feeling adventurous.
-
opened
cmd
and ranghci
, and played around to make sure it worked:C:\Users\Monkey>ghci GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Prelude> 1 + 1 2 Prelude> let x = 5 in x * x 25 Prelude> let f x = case x of 1 -> "hello"; _ -> "goodbye" in f 1 "hello" Prelude> let f x = case x of 1 -> "hello"; _ -> "goodbye" in f 45 "goodbye" Prelude> Leaving GHCi.
-
looked at learnhaskell and it said I should have cabal, so I ended up running
cabal update
thencabal install cabal cabal-install
, which seems a bit odd but it said to do that in Getting The Haskell Cabal. It seemed to work as the output includedInstalled Cabal-1.20.0.1
andInstalled cabal-install-1.20.0.3
.
First Steps
learnhaskell is telling me to do the “Yorgey course” first. I am informed that this will, in addition to equipping me to write Haskell, help me to understand parser combinators. I am glad about this, because the Wikipedia page for Parser combinator does not seem to be helping much with understanding. Perhaps I just lack education about combinators in general, and about context-free grammars.
Much of the first lecture is familiar from previous study. Key points:
Integer
is an integral type that is only limited by computer memory, whereasInt
is just a system integer that can only be guaranteed to deal with values up to +/- 2^29 (may be bigger depending on machine).-
backticks make prefix operators infix, parens make infix operators prefix:
1 + 2 == (+) 1 2 mod 2 1 == 2 `mod` 1 (%) = mod 2 `mod` 1 == 2 % 1
-
pattern matching is handy for functions. Expressions are checked in the order they are written:
factorial :: Integer -> Integer factorial 0 = 1 factorial 1 = 1 factorial n = n + factorial (n - 1)
-
guards use pipe character:
<functionName> <pattern> | <predicate expression> = <value> | <predicate expression> = <value> | <predicate expression> = <value>
e.g.
foo n | n < 0 = 0 | n < 10 = n + 10 | otherwise = n
-
function application has higher precedence than any infix operator, so
f 3 n+1 7
parses as(f 3 n) + (1 7)
-
make lists with
[1, 2, 3]
, or cons operator:1 : 2 : 3 : []
. They are singly linked lists (cons lists), with type[Integer]
. -
String is just sugar for [Char], so
['h', 'i'] == "hi"
Other than a reassurance about error messages, that is it for the lecture. Next is the first homework. It looks exciting, with credit card number validation and towers of Hanoi, among other less illustrated problems, but will have to wait until after I have had some sleep.
The course also recommends Chapter 2 of Learn You A Haskell and two Real World Haskell chapters: Chapter 1 and Chapter 2, so I will have a read of those.