# atmantree's solution

## to Hamming in the Haskell Track

Published at Jul 25 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution 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.

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

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.

## Getting Started

For installation and learning resources, refer to the exercism help page.

## Running the tests

To run the test suite, execute the following command:

``````stack test
``````

#### If you get an error message like this...

``````No .cabal file found in directory
``````

You are probably running an old stack version and need to upgrade it.

#### Otherwise, if you get an error message like this...

``````No compiler found, expected minor version match with...
Try running "stack setup" to install the correct GHC...
``````

Just do as it says and it will download and install the correct compiler version:

``````stack setup
``````

## Running GHCi

If you want to play with your solution in GHCi, just run the command:

``````stack ghci
``````

## Feedback, Issues, Pull Requests

The exercism/haskell repository on GitHub is the home for all of the Haskell 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!

## Source

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.

### Tests.hs

``````{-# OPTIONS_GHC -fno-warn-type-defaults #-}
{-# LANGUAGE RecordWildCards #-}

import Data.Foldable     (for_)
import Test.Hspec        (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import Hamming (distance)

main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs

specs :: Spec
specs = describe "distance" \$ for_ cases test
where

test Case{..} = it description assertion
where
assertion  = expression `shouldBe` fromIntegral <\$> expected
expression = distance strand1 strand2

data Case = Case { description :: String
, strand1     :: String
, strand2     :: String
, expected    :: Maybe Integer
}

cases :: [Case]
cases = [ Case { description = "empty strands"
, strand1     = ""
, strand2     = ""
, expected    = Just 0
}
, Case { description = "identical strands"
, strand1     = "A"
, strand2     = "A"
, expected    = Just 0
}
, Case { description = "long identical strands"
, strand1     = "GGACTGA"
, strand2     = "GGACTGA"
, expected    = Just 0
}
, Case { description = "complete distance in single nucleotide strands"
, strand1     = "A"
, strand2     = "G"
, expected    = Just 1
}
, Case { description = "complete distance in small strands"
, strand1     = "AG"
, strand2     = "CT"
, expected    = Just 2
}
, Case { description = "small distance in small strands"
, strand1     = "AT"
, strand2     = "CT"
, expected    = Just 1
}
, Case { description = "small distance"
, strand1     = "GGACG"
, strand2     = "GGTCG"
, expected    = Just 1
}
, Case { description = "small distance in long strands"
, strand1     = "ACCAGGG"
, strand2     = "ACTATGG"
, expected    = Just 2
}
, Case { description = "non-unique character in first strand"
, strand1     = "AAG"
, strand2     = "AAA"
, expected    = Just 1
}
, Case { description = "non-unique character in second strand"
, strand1     = "AAA"
, strand2     = "AAG"
, expected    = Just 1
}
, Case { description = "same nucleotides in different positions"
, strand1     = "TAG"
, strand2     = "GAT"
, expected    = Just 2
}
, Case { description = "large distance"
, strand1     = "GATACA"
, strand2     = "GCATAA"
, expected    = Just 4
}
, Case { description = "large distance in off-by-one strand"
, strand1     = "GGACGGATTCTG"
, strand2     = "AGGACGGATTCT"
, expected    = Just 9
}
, Case { description = "disallow first strand longer"
, strand1     = "AATG"
, strand2     = "AAA"
, expected    = Nothing
}
, Case { description = "disallow second strand longer"
, strand1     = "ATA"
, strand2     = "AGTG"
, expected    = Nothing
}
]``````
``````module Hamming (distance) where

compareItems :: (Char, Char) -> Bool
compareItems = uncurry  (/=)

distance :: String -> String -> Maybe Int
distance xs ys
| length xs /= length ys = Nothing
| otherwise              = Just \$ length \$ filter compareItems \$ zip xs ys``````