Question

I cannot figure out which case I am missing here,for my functions,can someone help me find which case to consider also so that I do not get the match non-exhaustive error.It is taking time for me to really figure out which part is missing.

fun eval_1 (nil,_) = nil
  | eval_1 (_,nil)  = nil 
  |eval_1 ((x::h::xs),(y::t::ys)) = if x = ~1 then (h,t)::eval_1(xs,ys) else(x,y)::eval_1(h::xs,t::ys);

fun eval_2 (nil,_) = nil
  | eval_2 (x,nil) = nil 
  |eval_2 ((x),(y::ys)) = eval_1 (x,y)::eval_2(x,ys);

 fun eval_3 (nil,_) = nil
   | eval_3 ((x::nil),nil) = nil 
   | eval_3 ((x::xs),(ys)) = eval_2(x,ys)::eval_3(xs,ys);
Was it helpful?

Solution

Matching on (x:xs, y:ys) instead of ((x::h::xs),(y::t::ys)) gets rid of the non-exhaustive warning. To make the rest of the code equivalent you can use hd and tl from List to get the head of the tail and the tail of the tail instead of decomposing the list with x::next::xs.

fun eval_1 (nil,_) = nil
  | eval_1 (_,nil) = nil 
  | eval_1 (x::xs, y::ys) =  if x = ~1 then (hd xs, hd ys)::eval_1(tl xs, tl ys) else (x,y) :: eval_1(xs, ys)

fun eval_2 (nil,_) = nil
  | eval_2 (x,nil) = nil 
  | eval_2 ((x),(y::ys)) = eval_1 (x,y)::eval_2(x,ys);

fun eval_3 (nil,_) = nil
  | eval_3 ((x::nil),nil) = nil 
  | eval_3 ((x::xs),(ys)) = eval_2(x,ys)::eval_3(xs,ys);

Sample run:

> val eval_1 = fn : int list * 'a list -> (int * 'a) list
val eval_2 = fn : int list * 'a list list -> (int * 'a) list list
val eval_3 = fn : int list list * 'a list list -> (int * 'a) list list list
val it = () : unit
> eval_1([1,2,3], [4,5,6]);
val it = [(1, 4), (2, 5), (3, 6)] : (int * int) list
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top