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?
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.
ghci, 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 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 included
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:
Integeris an integral type that is only limited by computer memory, whereas
Intis 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>
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 7parses 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
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.