Haskell 哈斯克尔使用地图
我想知道是否有可能在我的代码中使用映射,而不是像现在这样。我的代码是接收一个Haskell 哈斯克尔使用地图,haskell,Haskell,我想知道是否有可能在我的代码中使用映射,而不是像现在这样。我的代码是接收一个字符串,用其他字符串替换一些字符并将其反转,但我想在这段代码中使用映射 transforme' ::[Char] -> [Char] transforme' = reverse'.condiçoes condiçoes :: [Char] -> [Char] condiçoes = foldr (\x -> if x == 'A' then ('U':)
字符串
,用其他字符串替换一些字符
并将其反转,但我想在这段代码中使用映射
transforme' ::[Char] -> [Char]
transforme' = reverse'.condiçoes
condiçoes :: [Char] -> [Char]
condiçoes = foldr (\x -> if
x == 'A' then ('U':)
else if x == 'T' then ('A':)
else if x == 'C' then ('G':)
else if x == 'G' then ('C':)
else (x:)) []
reverse' :: [Char] -> [Char]
reverse' xs = foldr (\b f x -> f (b : x)) id xs []
您可以首先构造一个“翻译”函数:
trns 'A' = 'U'
trns 'T' = 'A'
trns 'C' = 'G'
trns 'G' = 'C'
trns x = x
我们如何使用:
condiçoes = map trns
因此,我们在trns
函数上执行映射
请注意,有一个内置的,或者您可以为此使用foldl
:
reverse' :: [a] -> [a]
reverse' = foldl (flip (:)) []
可能重复的