I want to make function named headcol
that works like:
headcol[[1,2],[3,4]] = [1,3];
So i made function like this:
fun headcol [] = []
| headcol [x::xs',y::ys'] = [x,y]
but when I call it, I get a match nonexhaustive.
IIUC, headcol
will extract all heads of lists in the parameter, whose type is 'a-list-list
. What you mathc is only []
and [x::xs', y::ys']
, not anything else. So, if your argument has more than 2 sublists, execption will raise:
- headcol[[1,2],[3,4], [4, 5, 9]];
uncaught exception Match [nonexhaustive match failure]
raised at: a.sml:9.34
-
If you just want to deal with two-elems list, pair is a better choice. Otherwise, you should match more cases:
fun headcol list =
case list of
[] => []
| x::xs' =>
case x of
[] => headcol xs'
| h::t => h::(headcol xs')
output:
- headcol[[1,2],[3,4], [4, 5, 9]];
- val it = [1,3,4] : int list