🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of ravicious

ravicious's solution

to RNA Transcription in the Elm Track

Published at Jul 07 2020 · 0 comments
Instructions
Test suite
Solution

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

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

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

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 RNATranscription exposing (toRNA)
import Test exposing (..)


isErr : Result error value -> Bool
isErr result =
    case result of
        Ok _ ->
            False

        Err _ ->
            True


tests : Test
tests =
    describe "RNATranscription"
        [ test "complement of cytosine is guanine" <|
            \() -> Expect.equal (Ok "G") (toRNA "C")
        , skip <|
            test "complement of guanine is cytosine" <|
                \() -> Expect.equal (Ok "C") (toRNA "G")
        , skip <|
            test "complement of thymine is adenine" <|
                \() -> Expect.equal (Ok "A") (toRNA "T")
        , skip <|
            test "complement of adenine is uracil" <|
                \() -> Expect.equal (Ok "U") (toRNA "A")
        , skip <|
            test "complement" <|
                \() -> Expect.equal (Ok "UGCACCAGAAUU") (toRNA "ACGTGGTCTTAA")
        , skip <|
            test "input \"INVALID\" should produce an error" <|
                \() -> Expect.true "expected an error message output. For example `Err \"Invalid input\"`" (toRNA "INVALID" |> isErr)
        ]
module RNATranscription exposing (toRNA)

-- We could implement the solution using a fold:
--
--     toRNA : String -> Result String String
--     toRNA =
--         String.foldr
--             (\char maybeString ->
--                 Maybe.map2
--                     String.cons
--                     (charToRnaNucleotide char)
--                     maybeString
--             )
--             (Just "")
--             >> Result.fromMaybe "Invalid input"
--
-- For the purposes of this exercise it'd be just fine!
-- However, if we had another requirement where we'd need
-- to process very long strands, then that solution
-- wouldn't be optimal.  When it encounters an invalid
-- character, it keeps folding over the strand anyway.
--
-- So I wanted to write a solution that stops processing
-- the strand at the first sight of an invalid character.
-- In Elm that involves writing a recursive function and
-- is a bit complicated.
--
-- There's a great post that goes into detail how
-- languages like Elm handle "loops" from which one would
-- wish to break out of at some point:
-- http://folkertdev.nl/blog/loops-to-folds/


toRNA : String -> Result String String
toRNA strand =
    toRNAhelper (Just "") strand
        |> Result.fromMaybe "Invalid input"


toRNAhelper : Maybe String -> String -> Maybe String
toRNAhelper maybeAcc rest =
    case maybeAcc of
        Nothing ->
            Nothing

        Just acc ->
            case String.uncons rest of
                Nothing ->
                    maybeAcc

                Just ( char, newRest ) ->
                    let
                        newAcc =
                            char
                                |> charToRnaNucleotide
                                |> Maybe.map String.fromChar
                                |> Maybe.map ((++) acc)
                    in
                    toRNAhelper newAcc newRest


charToRnaNucleotide : Char -> Maybe Char
charToRnaNucleotide char =
    case char of
        'G' ->
            Just 'C'

        'C' ->
            Just 'G'

        'T' ->
            Just 'A'

        'A' ->
            Just 'U'

        _ ->
            Nothing

Community comments

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

ravicious's Reflection

I decided to impose another requirement on myself: the function should stop processing the strand at the first sight of an invalid character.

It made the solution much more complicated, but I feel like it was worthwhile to investigate how such a solution could look like!