Avatar of sschellhoff

sschellhoff's solution

to Run Length Encoding in the OCaml Track

Published at May 27 2019 · 0 comments
Test suite


This exercise has changed since this solution was written.

Implement run-length encoding and decoding.

Run-length encoding (RLE) is a simple form of data compression, where runs (consecutive data elements) are replaced by just one data value and count.

For example we can represent the original 53 characters with only 13.


RLE allows the original data to be perfectly reconstructed from the compressed data, which makes it a lossless data compression.


For simplicity, you can assume that the unencoded string will only contain the letters A through Z (either lower or upper case) and whitespace. This way data to be encoded will never contain any numbers and numbers inside data to be decoded always represent the count for the following character.

Getting Started

For installation and learning resources, refer to the exercism help page.


To work on the exercises, you will need Opam and Base. Consult opam website for instructions on how to install opam for your OS. Once opam is installed open a terminal window and run the following command to install base:

opam install base

To run the tests you will need OUnit. Install it using opam:

opam install ounit

Running Tests

A Makefile is provided with a default target to compile your solution and run the tests. At the command line, type:


Interactive Shell

utop is a command line program which allows you to run Ocaml code interactively. The easiest way to install it is via opam:

opam install utop

Consult utop for more detail.

Feedback, Issues, Pull Requests

The exercism/ocaml repository on GitHub is the home for all of the Ocaml exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue. We'll do our best to help you!


Wikipedia https://en.wikipedia.org/wiki/Run-length_encoding

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


open Base
open OUnit2
open Run_length_encoding

let ae exp got _test_ctxt = assert_equal exp got ~printer:Fn.id

let encode_tests = [
   "empty string" >::
     ae "" (encode "");
   "single characters only are encoded without count" >::
     ae "XYZ" (encode "XYZ");
   "string with no single characters" >::
     ae "2A3B4C" (encode "AABBBCCCC");
   "single characters mixed with repeated characters" >::
   "multiple whitespace mixed in string" >::
     ae "2 hs2q q2w2 " (encode "  hsqq qww  ");
   "lowercase characters" >::
     ae "2a3b4c" (encode "aabbbcccc");

let decode_tests = [
   "empty string" >::
     ae "" (decode "");
   "single characters only" >::
     ae "XYZ" (decode "XYZ");
   "string with no single characters" >::
     ae "AABBBCCCC" (decode "2A3B4C");
   "single characters with repeated characters" >::
   "multiple whitespace mixed in string" >::
     ae "  hsqq qww  " (decode "2 hs2q q2w2 ");
   "lower case string" >::
     ae "aabbbcccc" (decode "2a3b4c");

let encode_and_then_decode_tests = [
   "encode followed by decode gives original string" >::
     ae "zzz ZZ  zZ" (encode "zzz ZZ  zZ" |> decode);

let () =
  run_test_tt_main (
    "run length encoding tests" >:::
      List.concat [encode_tests; decode_tests; encode_and_then_decode_tests]
open Base;;
let encode input =
    let char_tuples = List.map ~f:(fun x -> (1, x)) (String.to_list input)
    let rec combine_tuples = function
        | [] -> []
        | [t] -> [t]
        | ((n1, c1)::(n2, c2)::ts) -> if Char.equal c1 c2 then
            combine_tuples ( (n1 + n2, c1)::ts )
            (n1, c1)::( combine_tuples ( (n2, c2)::ts ) )
    let rec tuples_to_strings = function
        | [] -> []
        | (1, c)::ts -> (Char.to_string c)::(tuples_to_strings ts)
        | (n, c)::ts -> (Int.to_string n)::(Char.to_string c)::(tuples_to_strings ts)
    combine_tuples char_tuples |> tuples_to_strings |> String.concat

let decode input =
    let chars = String.to_list input
    let is_digit c = Char.to_int c >= Char.to_int '0' && Char.to_int c <= Char.to_int '9'
    let char_to_int c = Char.to_int c - Char.to_int '0'
    let rec make_tuples n = function
        | [] -> []
        | (c::cs) -> if is_digit c then
            make_tuples (n*10 + (char_to_int c)) cs
            (n, c)::(make_tuples 0 cs)
    let rec tuples_to_strings = function
        | [] -> []
        | (0, c)::cs -> (Char.to_string c)::(tuples_to_strings cs)
        | (n, c)::cs -> (String.init n ~f:(fun _ -> c))::(tuples_to_strings cs)
    make_tuples 0 chars |> tuples_to_strings |> String.concat

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?