COMP1011 Exercises for Week 03
Computing 1A 05s2 Last updated Tue 19 Jul 2005 14:37
Mail cs1011@cse.unsw.edu.au

# Tute Exercises Week 3

## Basic Control Structures & Data Types

When implementing the functions outlined below, make sure that you follow the Five Steps to Designing and Implementing a Function from the lecture.

1. #### Sort two numbers

Implement a function

`sort2 :: Int -> Int -> (Int, Int)`

that given two numbers returns a pair where the smaller of the two numbers is the first and the larger one the second component.

2. #### Computing Interest on an Account

The interest rates for a savings account are as follows:
• 0% per year on the first \$999
• 2% on every dollar from \$1000 to \$4,999
• 4% for every dollar from \$5000 to \$19,999
• 5% for every dollar from \$20,000
Write a function that computes the yearly interest for a given amount of money.

After an initial implementation without a `where` clause rewrite the definition to use a `where` clause to avoid the repeated statement of shared subcomputations.

3. #### Colour points

Assume the definition of colour points from the lecture:
```type Colour      = String
type ColourPoint = (Int, Int, Colour)```

1. Implement a function

`changeColour :: ColourPoint -> Colour -> ColourPoint`

that changes the colour of a colour point to the given value.

2. Implement a function
`equalPos :: ColourPoint -> ColourPoint -> Bool`
that checks if two color points have the same position.

4. #### Lists Versus Tuples

Why didn't we define

`type Point = [Int]`

in the lecture, but used a tuple instead?