🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # epequeno's solution

## to Hamming in the Elm Track

Published at Apr 19 2021 · 0 comments
Instructions
Test suite
Solution

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:

``````GAGCCTACTAACGGGAT
CATCGTAATGACGGCCT
^ ^ ^  ^ ^    ^^
``````

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 code you have to write is located inside the `src/` directory of the exercise. Elm automatically installs packages dependencies the first time you run the tests so we can start by running the tests from the exercise directory with:

``````\$ elm-test
``````

To automatically run tests again when you save changes:

``````\$ elm-test --watch
``````

As you work your way through the tests suite in the file `tests/Tests.elm`, be sure to remove the `skip <|` calls from each test until you get them all passing!

## Source

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.

### Tests.elm

``````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 String.length left /= String.length right then
Err "left and right strands must be of equal length"

else
Ok
(List.map2 areDifferent
(left |> String.toList)
(right |> String.toList)
|> List.sum
)

areDifferent : Char -> Char -> Int
areDifferent l r =
if l /= r then
1

else
0``````