Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of rootulp

rootulp's solution

to Hamming in the Elm Track

Published at Jul 13 2018 · 1 comment
Test suite


This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Calculate the Hamming difference between two DNA strands.

A mutation is simply a mistake that occurs during the creation or copying of a nucleic acid, in particular DNA. Because nucleic acids are vital to cellular functions, mutations tend to cause a ripple effect throughout the cell. Although mutations are technically mistakes, a very rare mutation may equip the cell with a beneficial attribute. In fact, the macro effects of evolution are attributable by the accumulated result of beneficial microscopic mutations over many generations.

The simplest and most common type of nucleic acid mutation is a point mutation, which replaces one base with another at a single nucleotide.

By counting the number of differences between two homologous DNA strands taken from different genomes with a common ancestor, we get a measure of the minimum number of point mutations that could have occurred on the evolutionary path between the two strands.

This is called the 'Hamming distance'.

It is found by comparing two DNA strands and counting how many of the nucleotides are different from their equivalent in the other string.

^ ^ ^  ^ ^    ^^

The Hamming distance between these two DNA strands is 7.

Implementation notes

The Hamming distance is only defined for sequences of equal length. This means that based on the definition, each language could deal with getting sequences of equal length differently.

Elm Installation

Refer to the Exercism help page for Elm installation and learning resources.

Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed.

$ elm-package install --yes

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's possible to submit an incomplete solution so you can see how others have completed the exercise.


module Tests exposing (..)

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")


    "version": "3.0.0",
    "summary": "Exercism problems in Elm.",
    "repository": "https://github.com/exercism/elm.git",
    "license": "BSD3",
    "source-directories": [
    "exposed-modules": [],
    "dependencies": {
        "elm-lang/core": "5.0.0 <= v < 6.0.0",
        "elm-community/elm-test": "4.0.0 <= v < 5.0.0"
    "elm-version": "0.18.0 <= v < 0.19.0"
module Hamming exposing (..)

distance: String -> String -> Maybe Int
distance strand1 strand2 =
    if equalLengthStrands strand1 strand2 then
        Just (strandDistance strand1 strand2)

equalLengthStrands: String -> String -> Bool
equalLengthStrands strand1 strand2 =
    String.length strand1 ==
    String.length strand2

strandDistance: String -> String -> Int
strandDistance strand1 strand2 =
    List.map2 nucleotideDistance
        (String.toList strand1)
        (String.toList strand2)
        |> List.sum

nucleotideDistance: Char -> Char -> Int
nucleotideDistance nucleotide1 nucleotide2 =
    if nucleotide1 == nucleotide2 then 0 else 1

Community comments

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

I like that you separated the validation logic (are the strands of equal length?) from the computation (nucleotideDistance)!

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?