Exercise 1
This exercise will be an introduction to working with the Haskell tools. For this course, we will be using the Glasgow Haskell Compiler. You can download it from their web site; it is installed in CSIL Linux; or install the Ubuntu package ghc
.
Some links to the GHC software have been provided. You will also find GHC on the CSIL Linux computers. The Haskell tutorials and references on the same page may be helpful as well.
Getting Started with GHCi
Start by getting GHC installed (or going to CSIL). We will start by using the GHC interactive environment. (Type ghci
at the Linux prompt to start it.) Once GHCi is started, you'll see a prompt like this:
Prelude>
You can start experimenting with Haskell at this prompt. Try typing some expressions at this prompt and look at the results. Some example things to try:
9 * 4
1 + 2 * 3
1+1 == 2
2^300
sum [1,2,3,4,5]
[1,2,3] ++ [4,5]
take 3 [5,6,7,8,9,10]
div 15 3
15 `div` 3
5 + 6
(+) 5 6
First Haskell Code
In your favourite text editor, create a file exer1.hs
containing the following three function definitions (that calculate the discriminant and two solutions to a quadratic formula):
det a b c = b^2 - 4*a*c
quadsol1 a b c = (-b - sqrt (det a b c))/2*a
quadsol2 a b c = (-b + sqrt (det a b c))/2*a
You can test it in GHCi like this:
Prelude> :l exer1.hs
*Main> det 1 4 2
8
*Main> quadsol1 1 4 2
-3.414213562373095
*Main> quadsol2 1 4 2
-0.5857864376269049
Writing Your First Code
Add another function to your exer1.hs
called third_a
that returns the third element of a list. It should behave like this: (including no need to check for the error if there is no third element)
*Main> :r
*Main> third_a [7,8,9,10,11,12]
9
*Main> third_a [1,2]
*** Exception…
The function third_a
must use the built-in list indexing operator.
Write another implementation of the same logic, third_b
that uses pattern matching to get to the third element. It should behave identically to third_a
. (The exception might be different, but should occur in the same cases.)
Factorial
Write a recursive function fact
that takes one argument and recursively calculates its factorial. That is,
*Main> fact 0
1
*Main> fact 5
120
*Main> fact 10
3628800
Hailstone Function
Finally, in your exer1.hs
, write a function hailstone n
that find the next element in the hailstone sequence. That is, for even n
, it should return n/2
; for odd n
, it should return 3n+1
. (Hint: the Haskell div
function does integer division; the /
always returns a Float
, which is not what you want.)
*Main> hailstone 14
7
*Main> hailstone 31
94
You must not use an if
expression (which isn't very Haskell-ish). Use a guarded expression.
Hailstone Length
Write a recursive function hailLen
(using pattern matching) that calculates the length of the hailstone sequence starting at the argument. (i.e. the number of values in the sequence before it gets to one)
*Main> hailLen 11
14
*Main> hailLen 27
111
*Main> hailLen 686901248
270
*Main> hailLen 1
0
You should not construct a list and calculate its length, but can (and should) use hailstone
.
Aside: it is not known whether or not this function halts for all inputs since it has not been proven that every hailstone sequence gets to one. But the sequence does terminate for all values up to \(2^{64}\), so that should get us through this exercise.
Submitting
Submit your files through CourSys for Exercise 1.