I am completely new to OCaml, so I am having some trouble with the basics. For my program, I have to match a nucleotide with it’s complement (G -> C, C -> G, A -> T, T -> A) in order to find the other half of the double helix. The general idea is that DNA is composed of 2 complementary helixes, each of which is a sequence of nucleotides. Currently, I am trying to compute the other half of the double helix.
So far, I have represented the nucleotides with an enumeration and I’ve represented the DNA with a nucleotide list which corresponds to one helix.
type nucleotide =
| G
| C
| A
| T
type helix = nucleotide list
let rec complementary_helix (x:helix): helix =
| [G] -> [C]
| [C] -> [G]
| [A] -> [T]
| [T] -> [A]
end
I know something is missing here, but I don’t know how to go about it. Can somebody steer me in the right direction?
You’re basically just missing
List.map:(For what it’s worth, it’s not necessary to specify types. OCaml will infer the types. It’s good style to specify them for documentation but maybe not if they’re obvious.)
Edit
OK, I guess this is a homework problem in which you’re supposed to use recursion to solve the problem.
The way to think of recursion is that you want to solve a little piece of the problem, which gives you a smaller problem to solve. You pass the smaller problem to yourself (either before or after you solve your little piece). You also need to know when the problem has gotten so small there’s no more work to do on it.
In your case, the little piece would be to translate one nucleotide to its complement. You’re doing that semi-OK (you have lists where you would really just want to work on single nucleotides). But you’re not passing the remainder of the problem to yourself to solve recursively. You’re also not checking whether the problem is so small there’s nothing to do.
For functions on lists, around 99% of the time you’re going to make the problem smaller by splitting the list into the head (a single element) and the tail (a list that’s smaller by one). That will work for you here.
Edit 2
As an example of how list recursion looks, here’s a function that adds up all the integers in a list:
This has all the parts I described. The
matchis used both to tell when the problem is trivial (when the list is empty) and to split the list into the head and the tail. Assuming you had the sum for the tail (which you can get recursively), the answer is pretty obvious. You just need to add the head onto this sum. You just need to ask yourself (almost always): if I had the answer for the tail of the list, what would I need to do to combine it with the head?