Tutorial Exercises Week 2
This week your tutor will bring printouts of this page, in future weeks you will need to bring your own printout.
Intro
Introduce yourselves, get to know your tutor. How can you contact them?
Haskell: writing simple functions, types
Do not worry if you do not complete all these exercises in the tute. Finish the remaining exercises at home as part of your weekly practise.
Q1. type signatures
The type of a variable determines what values it may take. Int means integers (whole numbers) up to a certain size, Integer means integers of any size, Float means floating point numbers (real numbers up to a certain accuracy), Bool means one of the special values True and False, Char means a single character eg a letter, a space, or a punctuation symbol.
In Haskell the type signature of a function describes the types of its inputs and its output.
For example the type signature f :: Int -> Int means that f is a function which takes one input (an Int) and returns an output which is also an Int. An example of such a function is the function which squares an integer.
Give examples of familiar functions with the following type signatures:
-
Int -> Bool
-
Int -> Float
-
Int -> Int -> Int
-
Float -> Int
-
Int -> Int -> Bool
-
Float
Q2. cube
Write a Haskell function cube which returns the input raised to the power of 3. Assume the input is a floating point number.
Q3. circumference
Write a function called circumference which, given the radius of a circle, calculates its circumference. Recall circumference = 2 pi r where r is the radius.
In your answer use the built-in Haskell function called pi which returns the floating point number 3.141592...
Q: Which of the type signatures in Q1 might be the type signature of the pi function?
Q4. mystery
It is often convenient to represent characters (letters, punctuation symbols, etc) as numbers. The built-in Haskell function Char.ord converts a character to the number which represents it. The function Char.chr converts a number back into the corresponding character.
Eg
Prelude> Char.ord 'a' 97 Prelude> Char.ord 'b' 98 Prelude> Char.ord 'z' 122 Prelude> Char.ord 'A' 65 Prelude> Char.ord 'B' 66 Prelude> Char.chr 65 'A' Prelude> Char.chr 66 'B'
Consider the following Haskell function:
-
mystery x = Char.chr ((Char.ord x) + 1)
What is its type signature? What does it do?
Tutorial Presentations
Your tutor will put you into pairs and allocate a date for your for your presentation. In tutorials with an odd number of students there will be one student presenting by themselves. Do not worry abou this, solo presentations will be assessed more leniently.You may choose your own partner if you wish.
Swap contact details with your partner.
Q5. isEven
There are some built in functions (==,>,<,>=,<=, /=) with type signature:
-
a -> a -> Bool
where a can be just about any type. For example
Prelude> 4 /= 4 False Prelude> 4 == 4 True Prelude> 4 > 4 False Prelude> 4 < 5 True
Using one of these functions write a haskell function called isEven which takes an integer as input and returns True if and only if it is even.
Hint: you may wish to use the `mod` operator. Notice mod is enclosed in backward quote symbols ` (this is not the quote symbol labelled " on your keyboard, it is the other quote symbol, usually on the top row of keys)
Q6. max3
Your tutor will show you how to use the | "alternative" symbol in your function definitions.
Using "|" write a function max3 which takes three integers as arguments and returns the largest of the three.
Here is how max3 should behave:
Tut02> :t max3 max3 :: Int -> Int -> Int -> Int Tut02> max3 4 5 6 6 Tut02> max3 9 9 9 9 Tut02> max3 7 7 5 7 Tut02> max3 9 2 1 9
Hint: Be careful that you cover all possible cases.
Self-test Question: Sum 2 of 3
Do this question at home after your tutorial as a self-test.
Write a Haskell function sum2of3 that takes three numbers and returns True if, and only if, the sum of any two of these numbers equals the third.
For example:
Tut02> :t sum2of3 sum2of3 :: Int -> Int -> Int -> Bool Tut02> sum2of3 3 4 5 False Tut02> sum2of3 3 8 5 True Tut02> sum2of3 1 2 3 True Tut02> sum2of3 8 1 8 False
