Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Viewing all articles
Browse latest Browse all 527

How to make tail function return what I need?

$
0
0

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'))]

Viewing all articles
Browse latest Browse all 527

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>