Given a DNA strand, return its RNA complement (per RNA transcription).
Both DNA and RNA strands are a sequence of nucleotides.
The four nucleotides found in DNA are adenine (A), cytosine (C), guanine (G) and thymine (T).
The four nucleotides found in RNA are adenine (A), cytosine (C), guanine (G) and uracil (U).
Given a DNA strand, its transcribed RNA strand is formed by replacing each nucleotide with its complement:
G
-> C
C
-> G
T
-> A
A
-> U
For installation and learning resources, refer to the exercism help page.
To run the test suite, execute the following command:
stack test
No .cabal file found in directory
You are probably running an old stack version and need to upgrade it.
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
If you want to play with your solution in GHCi, just run the command:
stack ghci
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!
Hyperphysics http://hyperphysics.phy-astr.gsu.edu/hbase/Organic/transcription.html
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
{-# LANGUAGE RecordWildCards #-}
import Data.Foldable (for_)
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)
import DNA (toRNA)
main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs
specs :: Spec
specs = describe "toRNA" $ for_ cases test
where
test Case{..} = it description $ toRNA dna `shouldBe` expected
data Case = Case { description :: String
, dna :: String
, expected :: Maybe String
}
cases :: [Case]
cases = [ Case { description = "RNA complement of cytosine is guanine"
, dna = "C"
, expected = Just "G"
}
, Case { description = "RNA complement of guanine is cytosine"
, dna = "G"
, expected = Just "C"
}
, Case { description = "RNA complement of thymine is adenine"
, dna = "T"
, expected = Just "A"
}
, Case { description = "RNA complement of adenine is uracil"
, dna = "A"
, expected = Just "U"
}
, Case { description = "RNA complement"
, dna = "ACGTGGTCTTAA"
, expected = Just "UGCACCAGAAUU"
}
, Case { description = "correctly handles invalid input (RNA instead of DNA)"
, dna = "U"
, expected = Nothing
}
, Case { description = "correctly handles completely invalid DNA input"
, dna = "XXX"
, expected = Nothing
}
, Case { description = "correctly handles partially invalid DNA input"
, dna = "ACGTXXXCTTAA"
, expected = Nothing
}
]
module DNA (toRNA) where
nucleotides = "ACGT"
complements = "UGCA"
toRNA :: String -> Maybe String
toRNA xs = traverse (\x -> lookup x $ zip nucleotides complements) xs
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.
Level up your programming skills with 3,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments