🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of hyphenrf

hyphenrf's solution

to Hamming in the OCaml Track

Published at May 14 2020 · 0 comments
Test suite

Calculate the Hamming Distance between two DNA strands.

Your body is made up of cells that contain DNA. Those cells regularly wear out and need replacing, which they achieve by dividing into daughter cells. In fact, the average human body experiences about 10 quadrillion cell divisions in a lifetime!

When cells divide, their DNA replicates too. Sometimes during this process mistakes happen and single pieces of DNA get encoded with the incorrect information. If we compare two strands of DNA and count the differences between them we can see how many mistakes occurred. This is known as the "Hamming Distance".

We read DNA using the letters C,A,G and T. Two strands might look like this:

^ ^ ^  ^ ^    ^^

They have 7 differences, and therefore the Hamming Distance is 7.

The Hamming Distance is useful for lots of things in science, not just biology, so it's a nice phrase to be familiar with :)

Implementation notes

The Hamming distance is only defined for sequences of equal length, so an attempt to calculate it between sequences of different lengths should not work. The general handling of this situation (e.g., raising an exception vs returning a special value) may differ between languages.

Getting Started

  1. Install the Exercism CLI.

  2. Install OCaml.

  3. For library documentation, follow Useful OCaml resources.

Running Tests

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


Submitting Incomplete Solutions

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

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 or submit a PR. We welcome new contributors!


The Calculating Point Mutations problem at Rosalind http://rosalind.info/problems/hamm/


(* hamming - 2.3.0 *)
open Base
open OUnit2
open Hamming

let printer = function
  | Error m -> "Error \"" ^ m ^ "\""
  | Ok x -> "Ok " ^ (Int.to_string x)

let ae exp got _test_ctxt = assert_equal ~printer exp got

let dna_of_string s =
  let f = function
    | 'A' -> A
    | 'C' -> C
    | 'G' -> G
    | 'T' -> T
    | _   -> failwith "Big news! New nucleotide discovered" in
  String.to_list s |> List.map ~f

let hamdist a b = hamming_distance (dna_of_string a) (dna_of_string b)

let tests = [
  "empty strands" >::
  ae (Ok 0) (hamdist "" "");
  "single letter identical strands" >::
  ae (Ok 0) (hamdist "A" "A");
  "single letter different strands" >::
  ae (Ok 1) (hamdist "G" "T");
  "long identical strands" >::
  ae (Ok 0) (hamdist "GGACTGAAATCTG" "GGACTGAAATCTG");
  "long different strands" >::
  ae (Ok 9) (hamdist "GGACGGATTCTG" "AGGACGGATTCT");
  "disallow first strand longer" >::
  ae  (Error "left and right strands must be of equal length") (hamdist "AATG" "AAA");
  "disallow second strand longer" >::
  ae  (Error "left and right strands must be of equal length") (hamdist "ATA" "AGTG");
  "disallow left empty strand" >::
  ae  (Error "left strand must not be empty") (hamdist "" "G");
  "disallow right empty strand" >::
  ae  (Error "right strand must not be empty") (hamdist "G" "");

let () =
  run_test_tt_main ("hamming tests" >::: tests)
type nucleotide = A | C | G | T

(* Single-pass function *)
let hamming_distance l r =
  match l, r with
  | [], [] -> Ok 0
  | [], _  -> Error "left strand must not be empty"
  | _ , [] -> Error "right strand must not be empty"
  | _ , _  -> 
    let ne a b = if a <> b then 1 else 0 in
    let rec count ?(n=0) = function
      | [], [] -> n
      | x::xs, y::ys -> count ~n:(n + (ne x y)) (xs, ys)
      | _ -> raise (Failure "left and right strands must be of equal length")
    try Ok (count (l, r))
    with Failure msg -> Error msg

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?