# 5 Higher order Functions on Lists

1. try:
```use list;

double:num -> num;
double(n) <= n*n;

map double [1,2,3,4];

map (lambda x => x*x) [1,2,3,4];
```
2. try

```use list;

type string == list(char);

type student == string X string;

type class == list(student X num);

c:class;

c <= [(("Fred","Jones"),66),(("Ali","Mohamed"),76),(("Mary","Ward"),92),(("Colin","Gold"),31)];

map (lambda ((x,y),z) => x) c;

map (lambda ((x,y),z) => z) c;

foldr  (0 ,(+))  (map (lambda ((x,y),z) => z) c);

(foldr  (0 ,(+))  (map (lambda ((x,y),z) => z) c))/length(c);

averageClass: class -> num;
averageClass k <= (foldr  (0 ,(+))  (map (lambda ((x,y),z) => z) k))/length(k);

averageClass	 c;

type course == string X class;

c1: course;

c1 <=("CS1", c);

c2: course;

c2 <=("Maths", [(("Fred","Jones"),36),(("Ali","Mohamed"),56),(("Mary","Ward"),45),(("Colin","Gold"),71)]);

averageCourse:course -> num;
averageCourse(x,y) <= averageClass(y);

averageCourse c1;

type degreeprogramme == list(course);

cs:degreeprogramme;
cs <= [c1,c2];

cs;

map averageCourse cs;
map (lambda (x,y) => (x, averageCourse (x,y))) cs;
```

3. Redo all last week's exercises using higher order functions.

4. Look at the files on igor in directory: `/usr/local/share/hope/lib`. That explains the point of `use list;` at the beginning of the above.

s.danicic@gold.ac.uk
Sebastian Danicic BSc MSc PhD (Reader in Computer Science)
Dept of Computing, Goldsmiths, University of London, London SE14 6NW
Last updated 2011-03-15