PDA

View Full Version : O'caml question


Strike
05-21-2003, 03:30 PM
Okay, I'm trying to translate my Haskel bubble sort into O'Caml and I'm not understanding why the interpreter doesn't like this:


# let rec bsorthelper lst =
match lst with
[] -> ([], false)
| [x] -> ([x], false)
| x :: xs ->
let (y::ys, changed) = bsorthelper xs in
if x > y then (y::x::ys, true) else (x::y::ys, changed);;

Warning: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
([], _)
val bsorthelper : 'a list -> 'a list * bool = <fun>
#

And it has the let clause underlined saying that it isn't exhaustive ... what gives?

GnuVince
05-21-2003, 08:07 PM
# let (y::ys, ch) = ([1;2;3], true);;
Warning: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
([], _)
val y : int = 1
val ys : int list = [2; 3]
val ch : bool = true
# let (mylist, ch) = ([1;2;3], true);;
val mylist : int list = [1; 2; 3]
val ch : bool = true
# List.hd [];;
Exception: Failure "hd".
# List.tl [];;
Exception: Failure "tl".
#


Your assignement fails if your y::ys recieves an empty list. Use a variable name and List.hd and List.tl in your if statement instead.