I need to encode list in OCaml. Command: encode ['a','a','b','b','b','c'];;
have to return [(2,'a');(3,'b');(1,'c')]
Now I have this function:
let encode list = let rec encodeHelper list acc = match list with | [] -> [] | head :: [] -> (1, head) :: [] | head :: headNext :: tail -> if head = headNext then encodeHelper (headNext :: tail) (acc + 1) else (acc, head) :: encodeHelper (headNext :: tail) acc in encodeHelper list 1;;
But it returns:
- : (int * (char * char * char * char * char * char)) list =[(1, ('a', 'a', 'b', 'b', 'b', 'c'))]