Getting started with Haskell

Chapter: Getting started with Haskell

There are many Haskells around. I'm going to encourage you to use the Glasgow Haskell Compiler (ghc) in interactive mode. You can get your own Compiler and Haskell Platform from http://hackage.haskell.org/platform/index.html.


Exercise 1

Download and install Haskell.

After installing just type "ghci" at the Unix prompt to enter the interactive ghci environment.

Just as in Scheme, you can type in expressions for Haskell to evaluate:

rhyspj% ghci
GHCi, version 7.0.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Prelude> 5751 * 47
270297
Prelude> 270297 / 47
5751.0
Prelude> 270297 `div` 47
5751
Prelude> div 270297 47
5751
Prelude> div 270296 47
5750
Prelude> 270296 / 47
5750.978723404255
Prelude> 

Some things to note:


Exercise 2

Investigate rem and `rem`. Write your conclusions.

Q. 1
If x is today's temperature in Fahrenheit, then subtracting 32 from x and multiplying the result by 5/9 should give today's temperature in Centigrade. How do you write this expression in Haskell if today's temperature is 42?


You can name that function like this:

Prelude> let ftoc x = (x-32)*5/9 in ftoc 42
5.55555555555556
Scope of variables in Haskell is "more generous" than in Scheme. "let" is more like letrec:
Prelude> let x=u+v; y=u-v; z=x/y; u=f 3; v=u-2; f x=x*x in z+1/z
8.125
or
Prelude> let x=3; fac 0 = 1; fac n = n*(fac (n-1)) in fac x
6
Prelude> let x=1000; fac 0 = 1; fac n = n*(fac (n-1)) in fac x
40238726007709377354370243392300398571937486421071463254379991042993851239862902
05920442084869694048004799886101971960586316668729948085589013238296699445909974
24504087073759918823627727188732519779505950995276120874975462497043601418278094
64649629105639388743788648733711918104582578364784997701247663288983595573543251
31853239584630755574091142624174743493475534286465766116677973966688202912073791
43853719588249808126867838374559731746136085379534524221586593201928090878297308
43139284440328123155861103697680135730421616874760967587134831202547858932076716
91324484262361314125087802080002616831510273418279777047846358681701643650241536
91398281264810213092761244896359928705114964975419909342221566832572080821333186
11681155361583654698404670897560290095053761647584772842188967964624494516076535
34081989013854424879849599533191017233555566021394503997362807501378376153071277
61926849034352625200015888535147331611702103968175921510907788019393178114194545
25722386554146106289218796022383897147608850627686296714667469756291123408243920
81601537808898939645182632436716167621791689097799119037540312746222899880051954
44414282012187361745992642956581746628302955570299024324153181617210465832036786
90611726015878352075151628422554026517048330422614397428693306169089796848259012
54583271682264580665267699586526822728070757813918581788896522081643483448259932
66043367660176999612831860788386150279465955131156552036093988180612138558600301
43569452722420634463179746059468257310379008402443243846565724501440282188525247
09351906209290231364932734975655139587205596542287497740114133469627154228458623
77387538230483865688976461927383814900140767310446640259899490222221765904339901
88601856652648506179970235619389701786004081188972991831102117122984590164192106
88843871218556461249607987229085192968193723886426148396573822911231250241866493
53143970137428531926649875337218940694281434118520158014123344828015051399694290
15348307764456909907315243327828826986460278986432113908350621709500259738986355
42771967428222487575867657523442202075736305694988250879689281627538488633969099
59826280956121450994871701244516461260379029309120889086942028510640182154399457
15680594187274899809425474217358240106367740459574178516082923013535808184009699
63725242305608559037006242712434169090041536901059339838357779394109700277534720
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000
Prelude> 

For the expression:

let v1 = e1
    v2 = e2
    ...
    vn = en
in e
the scope of each vi (i.e. the part of the program where it has value ei) consists of e together with all of the right hand sides e1, e2, ... ,en.

A similar construct is given by "where":

x = e where v1 = e1
            ...
            vn = en
where each vi has scope e, e1, ..., en.

Q. 2
Study the examples and rules above. I'll also give you the hint that Haskell uses static rather than dynamic scoping. What do you think is the value of
let f a = a * b where b = 3 in (let b = 0 in f 2) 


In Scheme, we might have done:

(define ftoc
  (lambda (x)
    (/ (* 5 (- x 32)) 9.0)))
and then been able to apply the function repetitively:
> (ftoc 19)
-7.222222222222222
> (ftoc -40)
-40.0
>
But we should not do this in Haskell.

Q. 3
Why not?


What we can do is to define our functions in a file. Suppose the file foo.hs contains the text

ftoc x = (x-32)*5/9
Then in our ghci session we can:
Prelude> :load foo.hs
:load foo.hs
[1 of 1] Compiling Main             ( foo.hs, interpreted )
Ok, modules loaded: Main.
*Main> ftoc 42
5.555555555555555
*Main>          


Exercise 3

Write Haskell code to calculate:

Now try some more investigations. With your knowledge of computer science you should be able to deduce some aspects of how Haskell deals with lists and strings. Unlike Scheme, lists in Haskell must be homogeneous -- all items must be of the same type. But there are also something called tuples that allow mixed types.


Exercise 4

Type in the following expressions. Study the results. Write up what you've learned:


rhyspj@gwu.edu