Gwahiir2761
Gwahiir2761

Reputation: 59

Pattern matching not exhaustive

New to learning OCaml and don't seem to get how to fix this question I found online.

(* Remove contiguous duplicates from a lost *)
# let rec destutter list =
    match list with 
    | [] -> []
    | hd1 :: hd2 :: tl -> 
        if hd1 = hd2 then destutter (hd2 :: tl)
        else hd1 :: destutter (hd2 :: tl)
    ;;

I've to enumerate all the cases, but not sure how to go about this.

Upvotes: 0

Views: 356

Answers (1)

Gwahiir2761
Gwahiir2761

Reputation: 59

Got it.

(* Remove contiguous duplicates from a lost *)
# let rec destutter list =
match list with 
| [] -> []
| [hd1] -> [hd1]
| hd1 :: hd2 :: tl -> 
    if hd1 = hd2 then destutter (hd2 :: tl)
    else hd1 :: destutter (hd2 :: tl)
;;

Upvotes: 3

Related Questions