I can't seem to figure out how to fully pattern-match this function. I want to check if a list is sorted using a function as an argument to say which order (inc or dec, < or >). I think my base code is pretty much there, but I am getting an error for: Uncaught SML exception: Match.
fun checkifsorted f =
fn nil => false
| [] => true
| a::b::c =>
if f(a, b) then checkifsorted f (b::c)
else false;
is_sorted (op >) [7,5,2,1];
If someone could point me to what I am missing in my pattern-matching section, it would be truly appreciated!
A quibble
You pattern match on both
niland[]. This is the same pattern.The problem
You then provide the pattern
a::b::cwhich matches a list with two or more elements. Eventually you will call your function with a list with a single element, and the incomplete pattern match will surface.Consider calling:
checkifsorted op< [1, 2, 3, 4, 5, 6]Cleaning up your code
You can clean this up a bit by not using
fnand just putting both parameters in the function definition and pattern-matching on them. I've also usedasto bindtlto the entire tail of the list, and_instead offin the case where it is irrelevant to the outcome.A solution
A list with a single element must be sorted, no? Presumably yes, so we can add a pattern in there to handle the single element list.