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

0xkr's solution

to RNA Transcription in the OCaml Track

Published at Apr 18 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

Getting Started

  1. Install the Exercism CLI.

  2. Install OCaml.

  3. For library documentation, follow Useful OCaml resources.

Running Tests

A Makefile is provided with a default target to compile your solution and run the tests. At the command line, type:

make

Submitting Incomplete Solutions

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

Feedback, Issues, Pull Requests

The exercism/ocaml repository on GitHub is the home for all of the Ocaml exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue or submit a PR. We welcome new contributors!

Source

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

test.ml

open Base
open OUnit2

let char_of_variant = function
  | `A -> 'A' | `C -> 'C' | `G -> 'G' | `T -> 'T' | `U -> 'U'
let printer l = List.map ~f:char_of_variant l |> String.of_char_list
let ae exp got _test_ctxt = assert_equal ~printer exp got

let tests =
  ["transcribes empty list">::
   ae [] (Rna_transcription.to_rna []);
   "transcribes cytidine">::
   ae [`G] (Rna_transcription.to_rna [`C]);
   "transcribes guanosine">::
   ae [`C] (Rna_transcription.to_rna [`G]);
   "transcribes adenosie">::
   ae [`U] (Rna_transcription.to_rna [`A]);
   "transcribes thymidine">::
   ae [`A] (Rna_transcription.to_rna [`T]);
   "transcribes multiple">::
   ae [`U; `G; `C; `A; `C; `C; `A; `G; `A; `A; `U; `U]
     (Rna_transcription.to_rna [`A; `C; `G; `T; `G; `G; `T; `C; `T; `T; `A; `A])
  ]

let () =
  run_test_tt_main ("rna-transcription tests" >::: tests)
type dna = [ `A | `C | `G | `T ]
type rna = [ `A | `C | `G | `U ]

(* This gives answer that is reverse of what we want *)
let rec trans_tail dna a =  
    match dna with
    | [] -> a
    | x :: xs -> 
        begin match x with 
            | `G -> trans_tail xs (`C::a)
            | `C -> trans_tail xs (`G::a)
            | `T -> trans_tail xs (`A::a)
            | `A -> trans_tail xs (`U::a)
        end

let rec trans dna = 
    match dna with
    | [] -> []
    | x :: xs -> 
        begin match x with 
            | `G -> `C :: trans xs
            | `C -> `G :: trans xs
            | `T -> `A :: trans xs
            | `A -> `U :: trans xs
        end

(* Just mapping would be good enough actually. I am overdoing tail recursion. 
Is map implementation tail recursive? Looks like its not. 

```
let rec map f = function
    [] -> []
  | a::l -> let r = f a in r :: map f 
```

Should benchmark all three. See which one is da best!!! 

Looks like there is List.rev_map which is tail recursive. Who reads docs anyway :p 

 *)


let to_rna dna =
    List.rev (List.rev_map (fun x -> 
        match x with 
          | `G -> `C 
          | `C -> `G 
          | `T -> `A 
          | `A -> `U 
    ) dna)

Community comments

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

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?