COMP1011 Exercises for Week 02
Computing 1A 05s2 Last updated Mon 01 Aug 2005 01:29

Lab Exercises, Week 2

Haskell & Basic Programming

Marking week 1 exercises

This lab follows on from the Week 1 lab exercises, review that lab before starting. (If you haven't done Lab 1 yet, do it now before continuing.)

Your tutor will check your week 1 work at the start of this lab. While you are waiting for them to get to you, log in and try to solve this week's exercises.

General rules for marking lab exercises

If you do not manage to finish labs on time, make sure that you seriously attempt to solve the exercises by yourself before studying the model solution posted the following week. The principal aim of the lab exercises is for you to practice programming. If you do not practice seriously, you will find the labs of following weeks increasingly difficult to solve and run the risk of failing this subject.

Ex 0: Registering at the forum

Go to the COMP1011 forum by following this link. Under the heading General, there is a topic Administration. Click on it. You will find a thread called "How to register in the forum". Select it, and read the instructions. Follow these instructions to register. If you get stuck, ask your tutor what to do.

Important: Please register with the forum as soon as possible. The forum is your main medium of support for assignments questions and other problems that you may have.

Ex 1: Using GHCi as a calculator

Start GHCi (type ghci in a terminal window) and see if you can get GHCi to calculate the answers to the following:

  1. Multiply 12345 by 67890
  2. Add 2468 and 13579
  3. Multiply the first 5 numbers (eg 1 times 2 times 3 times 4 times 5)
  4. Multiply the first 30 numbers
  5. Add the numbers from 1 to 100
  6. Add the odd numbers from 1 to 99
  7. What is the remainder when 456 is divided by 10?
  8. What is the remainder when 365 is divided by 7?
  9. Evaluate when a is 11, b is 20, c is 3.
  10. Produce the first letter of the string "first".
  11. Drop the first letter from the string "rest".
  12. Get the second letter of the string "Haskell".

If you have any troubles ask your neighbour(s) for help. If you all have troubles ask the tutor who will be walking around the lab checking that everyone is OK.

When you are finished leave the lines you typed and the answers visible in the xterm to show to your tutor when you have finished the lab. Do the remainder of the lab using another terminal window.

Ex 2: Your first program

Start the XEmacs editor and create a new file with the name Lab02.hs (files containing Haskell programs always end on the suffix .hs). If you don't know how to create a file with XEmacs, go back to Lab 1, where it has been covered.

In the lecture, we defined the function average. Enter it's definition into XEmacs, call the file Lab02.hs and save it:

module Lab02

average     :: Float -> Float -> Float
average a b  = (a + b) / 2.0

Then, start GHCi (in the same directory, in which you started Emacs) and load the program file by typing :load Lab02.hs (or just :l Lab02.hs). Test the function by computing the average of a pair of numbers.

Now go back to XEmacs and add another function definition. Define the function average3, which takes three numbers and computes their average. You need to consider two points: What is the type signature of average3? How can you define the calculation of the average of three numbers in Haskell?

When you have completed the above exercises show them to your tutor for this week's lab mark.

Additional Exercise

The following is an additional exercise to help you getting practice in programming. It is not part of the lab mark, but it should help you understand some of the material covered in the lecture.

Define a third function greeting, which given a person's name as an argument, prefixes the name with the string "Hello ". For example,

Main> greeting "Manuel"
"Hello Manuel"

Now extend this function by implementing yet another function longGreeting that behaves as follows:

Main> longGreeting "Manuel"
"Hello Manuel!  How are you?"

What is the type of each of these two functions?