Reading: Webster Ch. 5
Write a function max
of type int list -> int
that returns the largest element of a list of integers. Your function need not behave well if the list is empty. Hint: Write a helper function maxhelper
that takes as a second parameter the largest element seen so far. Then you can complete the exercise by defining:
fun max x = maxhelper (tl x, hd x);
Write a function fourth
of type 'a list -> 'a
that returns the fourth element of a list. Your function need not behave well on lists with less than four elements.
Exercise 1:
fun maxhelper ([], n) = n
| maxhelper ((x::xs), n) =
if x > n then maxhelper (xs, x)
else maxhelper (xs, n);
fun max x = maxhelper (tl x, hd x);
Exercise 2:
fun fourth x = hd (tl (tl (tl x)));
Write a function cuber
of type real -> real
that returns the cube of its parameter.
Write a function thirds
of type string -> char
that returns the third character of a string. Your function need not behave well on strings with lengths less than 3.
Write a function del3
of type 'a list -> 'a list
whose output list is the same as the input list, but with the third element deleted. Your function need not behave well on lists with lengths less than 3.
Write a function max3
of type int * int * int -> int
that returns the max value of a set of three values.