Here is how one might get started writing the recursive definition that Andreas Rossberg hints at.
fun f [] = ...
| f ((x,y)::pairs) = ...
This is one basis for a function f
that takes a list of pairs. It uses pattern matching to exhaust all possible input values by first splitting lists into empty and non-empty lists, and further splitting non-empty lists into "a head being a pair of x
and y
and a tail of remaining pairs beyond the first pair, being pairs
.
There are many ways you can write this. Another one is using case-of:
fun f pairs =
case pairs of
[] => ...
| (x,y) :: rest => ...
Here because pairs
is all pairs, and the pattern matching in case-of still splits the non-empty list into a head and a tail, I want to avoid overlapping the full input list, pairs
, with the remaining pairs beyond the first pair, rest
. So I gave them separate names.
It sounds like what would probably benefit you the most at this point is read more of your textbook on pattern matching and ask a tutor of some kind.