This assignment is from Induction to Programming using sml 5.14
Here is my code but I get this message pattern matching is not exhaustive in in base case..
fun revrev [] = [[], []]
| revrev [x::xs,y::ys] = revrev[ys@[y], xs @ [x]];
val test1revrev = revrev [[1, 2],[3, 4, 5]];
From valtest1 I want the output [[5, 4, 3], [2, 1]]
I fail to see why my function doesn’t work and need a little insight maybe.
For instance this works…
fun rev [] = []
| rev (x::xs) = rev1 xs @ [x];
val test1rev = rev [1, 2, 3];
I get [3, 2, 1]
Pattern matching isn’t exhaustive because you only match either empty list or list with two elements.
I think the intention of the exercise is to implement
revrevbased onrevwhich has been introduced before. Here are a few tips:x::xs. Similar to implementation ofrev, you can callrevrevonxsand put a transformation ofxat the end. This time you need to userevso thatxitself is also reversed.EDIT:
Your function doesn’t work in general cases. What I meant is the following skeleton:
where
xis also a list. Since this function is very similar torev, I hope that you can fill in...by yourself.