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

Tute Exercises Week 5

More Recursion & Lists

Please make sure that you have carefully read the specification for Assignment 1. Make a list of all the points that you don't understand and ask your tutor about them.

Delete characters from a list

Write a recursive function delete which, given a string and a character, returns the string with all occurances of the character removed.

For example:

Tut05> delete 'x' "x-files sux"	
"-files su"

Substitute characters in a list

In a similar style as delete, write a function substituting a given character with another character (instead of deleting it):

Tut05> substitute 'e' 'i' "eigenvalue"
"iiginvalui"

Shortest and longest string in a list

Write a function that returns both the shortest and the longest string in a list:

Tut05> shortestAndLongest ["abc"]
("abc","abc")
Tut05> shortestAndLongest ["This", "sentence", "is", "ridiculous"]
("is","ridiculous")

What do you think should happen when an empty list is passed to shortestAndLongest?

Lookup all properties

Write a function that yields a list of all the properties associated with all occurrences of a key in an association list:

Tut05> lookupAll "Plates" [("Forks", 10), ("Plates", 5), ("Cups", 0), ("Plates", 1)]
[5,1]
Tut05> lookupAll "Knives" [("Forks", 10), ("Plates", 5), ("Cups", 0), ("Plates", 1)]
[]

It is sufficient if the function works only for association lists of type [(String, Int)].

Deleting characters revisited

Is their anything that prevents delete to be used on lists of integers? What type do you have to give delete, so that a single implementation works on lists of characters (ie, strings) and list of numbers?

Tracing

Discuss the type signatures of

trace ::           String -> a -> a
show  :: Show a => a -> String

Assignment 1 - Style

Discuss the style requirements for Assignment 1.

  1. How should you write your program to get full style marks?
  2. What are common style mistakes?
  3. Why bother with style - is it any use except for assignment marks?