问题描述
我试图做一个DNA转录程序,但是我遇到了麻烦,我确信有一个更简单的方法可以做到这一点,但这是我头上的第一件事情但它不能按我想要的方式工作。 dnaToRna :: [Char] - > [char]
dnaToRna [] = []
dnaToRna xs = reverse(transc xs)
其中transc =(replaceA。replaceT。replaceC。replaceG)
replaceA = map(\\ \\ c - > if c =='A'then'U'else c)
replaceT = map(\c - > if c =='T'then'A'else c)
replaceC = map(\c - > if c =='C'then'G'else c)
replaceG = map(\c - > if c =='G'then'C'其他c)
以下是输出:
*主> let seq =AAATGTTAGTACACTAAGG
* Main> dnaToRna seq
GGUUUGUGUUGUUUGUUUU
我认为这是因为transc取代了A,然后检查整个字符串并替换T等等
任何提示?
在此先感谢!
您应该创建一个处理所有 Char - > Char
转换一次。
dnaToRna :: [Char] - > [Char]
dnaToRna =反向。 map transc
其中
transc'A'='U'
transc'T'='A'
transc'C'='G'
transc'G '='C'
transc _ =错误DNA分子无效
甚至更安全,你可以让它返回一个 Maybe [Char]
来代替。 函数也可以用来代替自定义映射函数。
dnaToRna :: [ Char] - >也许[Char]
dnaToRna = mapM(`lookup` zipATCGUAGC)。反向
I'm trying to make a DNA transcription program but I'm having trouble with the way I'm doing it, I'm sure there's an easier way to do this but this was the first thing that came to my head but it's not working the way I want it to.
dnaToRna :: [Char] -> [Char]
dnaToRna [] = []
dnaToRna xs = reverse(transc xs)
where transc = (replaceA . replaceT . replaceC . replaceG)
replaceA = map(\c -> if c == 'A' then 'U' else c)
replaceT = map(\c -> if c == 'T' then 'A' else c)
replaceC = map(\c -> if c == 'C' then 'G' else c)
replaceG = map(\c -> if c == 'G' then 'C' else c)
Here's the output:
*Main> let seq = "AAATGTTAGTACACTAAGG"
*Main> dnaToRna seq
"GGUUUGUGUUGUUUGUUUU"
I figure this is because the transc replaces the A, then checks the whole String and replaces the T, etc etcAny tips?Thanks in advance!
You should make one function which handles all of the Char -> Char
conversions at once.
dnaToRna :: [Char] -> [Char]
dnaToRna = reverse . map transc
where
transc 'A' = 'U'
transc 'T' = 'A'
transc 'C' = 'G'
transc 'G' = 'C'
transc _ = error "Invalid DNA molecule"
To make this even safer, you could make it return a Maybe [Char]
instead. The lookup
function can also be used instead of using a custom mapping function.
dnaToRna :: [Char] -> Maybe [Char]
dnaToRna = mapM (`lookup` zip "ATCG" "UAGC") . reverse
这篇关于结合多种功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!