1
votes

I have been trying one of the problems on 99 ocaml problems where you have to a have a list of all consecutive numbers in a list such as [2;3;4;4;5;6;6;6] -> [[2];[3];[4;4];[5];[6;6;6]]

let rec tail = function
     | [] -> []
     | [x] -> [x]
     | x::xs -> tail xs;;

let pack lst =
    let rec aux current acc = function
        | [] -> current
        | [x] -> if ((tail acc)=x) then ((x::acc)::current) else ([x]::current)
        | x::y::xs -> if (x=y) then aux (current) (x::acc) (y::xs) else aux (acc::current) ([]) (y::xs)
in
aux [] [] lst;;

When i run this i get the error

Error: This expression has type 'a list
but an expression was expected of type 'a list list
The type variable 'a occurs inside 'a list

I was wondering what the problem is? Thanks for any help it will be greatly valued

1
(tail acc)=x seems to be the culpritBergi
Ahh yes of course thank you very muchjames king

1 Answers

0
votes

As Bergi pointed out (tail acc)=x is the problem. tail returns 'a list so x must also be of type 'a list. The following x::acc then infers that acc must be of type 'a list list. But tail acc infers acc as 'a list.

At this point ocaml can't unify the tpyes of 'a list list and 'a list and gives the error you see.