Avatar of agbell

agbell's solution

to RNA Transcription in the Haskell Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

Note:

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.

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

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

Hyperphysics http://hyperphysics.phy-astr.gsu.edu/hbase/Organic/transcription.html

Submitting Incomplete Solutions

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

Tests.hs

{-# 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
               }
        ]
{-# LANGUAGE LambdaCase #-}
module DNA ( toRNA) where

toRNA :: String -> String
toRNA = fmap $ \case
   'G' -> 'C'
   'C' -> 'G'
   'T' -> 'A'
   'A' -> 'U'
   a -> error ("only GCTA chars are valid.  got " ++ show a)

Community comments

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

LambdaCase iteration

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?