Avatar of paulfioravanti

paulfioravanti's solution

to Hamming in the Elm Track

Published at Jul 07 2019 · 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.

Elm Installation

Refer to the Installing Elm page for information about installing elm.

Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

$ elm-test

Automatically run tests again when you save changes:

$ elm-test --watch

As you work your way through the test suite, be sure to remove the skip <| calls from each test until you get them all passing!


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

Submitting Incomplete Solutions

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


module Tests exposing (tests)

import Expect
import Hamming exposing (distance)
import Test exposing (..)

tests : Test
tests =
    describe "Hamming"
        [ test "identical strands" <|
            \() -> Expect.equal (Ok 0) (distance "A" "A")
        , skip <|
            test "long identical strands" <|
                \() -> Expect.equal (Ok 0) (distance "GGACTGA" "GGACTGA")
        , skip <|
            test "complete distance in single nucleotide strands" <|
                \() -> Expect.equal (Ok 1) (distance "A" "G")
        , skip <|
            test "complete distance in small strands" <|
                \() -> Expect.equal (Ok 2) (distance "AG" "CT")
        , skip <|
            test "small distance in small strands" <|
                \() -> Expect.equal (Ok 1) (distance "AT" "CT")
        , skip <|
            test "small distance" <|
                \() -> Expect.equal (Ok 1) (distance "GGACG" "GGTCG")
        , skip <|
            test "small distance in long strands" <|
                \() -> Expect.equal (Ok 2) (distance "ACCAGGG" "ACTATGG")
        , skip <|
            test "non-unique character in first strand" <|
                \() -> Expect.equal (Ok 1) (distance "AGA" "AGG")
        , skip <|
            test "non-unique character in second strand" <|
                \() -> Expect.equal (Ok 1) (distance "AGG" "AGA")
        , skip <|
            test "large distance" <|
                \() -> Expect.equal (Ok 4) (distance "GATACA" "GCATAA")
        , skip <|
            test "large distance in off-by-one strand" <|
                \() -> Expect.equal (Ok 9) (distance "GGACGGATTCTG" "AGGACGGATTCT")
        , skip <|
            test "empty strands" <|
                \() -> Expect.equal (Ok 0) (distance "" "")
        , skip <|
            test "disallow first strand longer" <|
                \() -> Expect.equal (Err "left and right strands must be of equal length") (distance "AATG" "AAA")
        , skip <|
            test "disallow second strand longer" <|
                \() -> Expect.equal (Err "left and right strands must be of equal length") (distance "ATA" "AGTG")
module Hamming exposing (distance)

distance : String -> String -> Result String Int
distance left right =
    if left == right then
        Ok 0

    else if sameLength left right then
            hammingDistance =
                    |> String.toList
                    |> zip (String.toList left)
                    |> List.foldl incrementDifference 0
        Ok hammingDistance

        Err "left and right strands must be of equal length"


sameLength : String -> String -> Bool
sameLength left right =
    String.length left == String.length right

zip : List Char -> List Char -> List ( Char, Char )
zip leftList rightList =
    List.map2 Tuple.pair leftList rightList

incrementDifference : ( Char, Char ) -> Int -> Int
incrementDifference ( leftNucleotide, rightNucleotide ) acc =
    if leftNucleotide /= rightNucleotide then
        acc + 1


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?