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:
Your test data as shown at the top is not in the right form.
A list in OCaml has elements separated by semicolons (;
). Your test data is using commas instead (,
). Commas are used for tuples, which is why you're seeing a tuple in your result.
If you change ,
to ;
in your test data you should see something closer to what you're looking for. There is at least one problem left to fix (in my testing).