CA215 Languages and
Computability
Functional Programming Lab
2: Defining Functions
Aim
The aim of
this practical is to help you learn to define simple Haskell functions.
Also,
you will be introduced to some common error messages produced by Hugs.
1. Area of
a Triangle
The area of
a triangle with sides a, b, c is given by the
formula:
_______________
√s(s
- a)(s - b)(s - c)
where
s = (a
+ b + c)/2
Design a
Haskell function triangleArea to calculate the area of a triangle given the
The type of
triangleArea should be Float -> Float -> Float
-> Float
The
behaviour of triangleArea should be
as follows:
> triangleArea 3 4 5
6.0
> triangleArea 1 2 2.5
0.949918
> triangleArea 1 1 (sqrt 2)
0.5
2. Sum Test
Design a
Haskell function isSum that takes
three integer arguments and tests whether one of them is the sum of the
other
two.
The
behaviour of isSum should be
as follows:
> isSum 1 2 3
True
> isSum 4 9 5
True
> isSum 12 5 7
True
> isSum 23 23 23
False
You should
start by declaring the type of isSum in your
script.
3.
Hugs
Error
Messages
The purpose
of this exercise is to show you some common Hugs error messages and to
Note that
getting used to the error messages and what they really mean is an
important
4.
Fibonacci Numbers
Early in
the 13th Century, Leonardo Fibonacci described the following
sequence:
0, 1, 1, 2,
3, 5, 8, 13, 21, 34, 55, 89, …
Except for
the first two numbers in the sequence, each is the sum of the two
numbers
>
fibonacci 1
0
>
fibonacci 2
1
>
fibonacci 3
1
>
fibonacci 8
13
>
fibonacci 20
4181
You should
take a similar approach to that used in the factorial function shown in
factorial :: Int -> Int
factorial n
|
n ==
0 = 1
|
otherwise
= n * (factorial (n-1))
fibonacci is more complicated than factorial, so here is
a hint: your function definition will need to handle three cases: n = 1, n = 2 and n > 2.
5. Area of
a Triangle (revisited)
Have you
considered what your triangleArea function
from exercise 1 will do with invalid data? For example, there is no
triangle
with sides 1, 2, 4. What does Hugs give as the value of the expression:
triangleArea
1 2 4?
Add to your
function definition some checks to handle such invalid data and report
an
error “Not a triangle!”
The isSum function from exercise 2 is also pretty close
to what you need for checking that three numbers can really be the
sides of a triangle. Begin by modifying this
6.
Fibonacci (revisited)
It would be
better to define the fibonacci function
using patterns rather than guards. Try to do this.