Avatar of Sendell

Sendell's solution

to Run Length Encoding in the OCaml Track

Published at Mar 09 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 ( >> ) f g x = g (f x)

let encode =
  let encode_char acc c =
    match acc with
    | [] -> [(1, c)]
    | (accL, accC) :: accT when Char.equal c accC -> (accL + 1, c) :: accT
    | acc -> (1, c) :: acc
  let tuple_to_string (n, c) =
    let c = Char.escaped c in
    if n = 1 then c else Int.to_string n ^ c
  >> List.fold_left ~init:[] ~f:encode_char
  >> List.map ~f:tuple_to_string
  >> String.concat

let decode =
  let char_list_to_int =
    List.map ~f:Char.get_digit_exn
    >> List.fold ~init:0 ~f:(fun acc n -> (10 * acc) + n)
  let decode_string l =
    let rec aux acc l =
      match List.split_while ~f:Char.is_digit l with
      | [], [] -> acc
      | [], c :: tl -> aux ((1, c) :: acc) tl
      | ns, c :: tl -> aux ((char_list_to_int ns, c) :: acc) tl
      | _, [] -> failwith "Ending with numbers should not happen"
    aux [] l
  String.to_list >> decode_string >> List.rev
  >> List.map ~f:(fun (n, c) -> String.make n c)
  >> String.concat


val encode : string -> string

val decode : string -> string

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?