Haskell - 是否存在替换函数? [英] Haskell - Does a replace function exist?
问题描述
我不知道,是否有像其他语言一样的替换函数。我搜索了一下,但不幸的是没有成功: - (
)所以我的尝试还很薄弱。 strong> 1st函数:
$ b
replace :: String - > String - > String - > String
替换findStr replaceStr myText = replace()??
函数:
replace :: String - > String - > String - > String
替换[] old new = []
替换str old new = loop str
其中
loop [] = []
loop str =
let (prefix,rest)= splitAt n str
in
if old == prefix - found a occurrence?
then new ++ loop rest - yes:replace
else head str:loop(tail str) - no:继续寻找
n = long old
第二个功能:
replaceBasedIdx ::字符串 - > [字符串] - >字符串 - >字符串
replaceBasedIdx findStr replaceStrList myText = replace()???
该函数应该将myTxt中的第1个findStr替换为replaceStrList的第1个元素,第2个findStr与第二元素等等...
示例:
replaceBasedIdxa[G,V,X]Haskell is a language
HGskell is V lXnguage
第二种功能的方法:
replaceBasedIdx :: String - > [字符串] - >字符串 - > String
replaceBasedIdx findStr replaceStrList myText = replaceBasedIdxSub findStr replaceStrList myText 0
replaceBasedIdxSub :: String - > [字符串] - >字符串 - > Int - > String
replaceBasedIdxSub findStr replaceStrList myText counter = loop myText
其中
loop [] = []
循环myText =
let(prefix,rest)= splitAt n myText $
中的b $ b如果findStr ==前缀 - 找到一个出现?
then(replaceStrList !!(counter + 1))++循环休息 - 是:替换它
else head myText:loop(tail myText) - no:继续寻找
n = length findStr
我现在已经非常接近最终结果,计数器不会增加。
您可以告诉我,我的错误在哪里?
我该如何修改第一个或第二个函数来获得第三个函数?
第三个函数:
replaceBasedIdxMultiple :: [String] - > [字符串] - >字符串 - >字符串
replaceBasedIdxMultiple findStrList replaceStrList myText = replace()???
该函数应该将myTxt中的findStrList的每个元素替换为replaceStrList中的相应元素,与1.,2.与2.等等...
示例:
replaceBasedIdxMultiple [A,X,G] [N,Y,K]ABXMG
NBYMK
你能帮我解决这个问题吗?一些提示和提示,如何开始它?
我真的不同: - (
谢谢很多提前
亲切的问候!
解决方案replace exists在
Data.List.Utils
中,MissingH
包的一部分。
实际上,这是一个非常简洁的实现:
replace :: Eq a => [a] - > [ a] - > [a] - > [a]
替换old new = join new。split old
I have to make three functions for replacing of flat strings and in lists.
I don't know, whether there is a replace function like in other languages. I searched for that however unfortunately without success :-(
So my attempt is yet quite thin.
1st function:
replace :: String -> String -> String -> String replace findStr replaceStr myText = replace()??
My approach for the 1st function:
replace :: String -> String -> String -> String replace [] old new = [] replace str old new = loop str where loop [] = [] loop str = let (prefix, rest) = splitAt n str in if old == prefix -- found an occurrence? then new ++ loop rest -- yes: replace else head str : loop (tail str) -- no: keep looking n = length old
2nd function:
replaceBasedIdx :: String -> [String] -> String -> String replaceBasedIdx findStr replaceStrList myText = replace()???
This function should replace the 1st findStr in myTxt with the 1st element of replaceStrList, the 2nd findStr with the 2nd element and so on...
Example:
replaceBasedIdx "a" ["G","V","X"] "Haskell is a language" "HGskell is V lXnguage"
My approach for the 2nd function:
replaceBasedIdx :: String -> [String] -> String -> String replaceBasedIdx findStr replaceStrList myText = replaceBasedIdxSub findStr replaceStrList myText 0 replaceBasedIdxSub :: String -> [String] -> String -> Int -> String replaceBasedIdxSub findStr replaceStrList myText counter = loop myText where loop [] = [] loop myText = let (prefix, rest) = splitAt n myText in if findStr == prefix -- found an occurrence? then (replaceStrList !! (counter+1)) ++ loop rest -- yes: replace it else head myText : loop (tail myText) -- no: keep looking n = length findStr
I'm now very near to the final result, however the counter doesn't increment.
Could you please tell me, where my mistake is? And how could I modifey the 1st or 2nd function to get the 3rd function also?
3rd function:
replaceBasedIdxMultiple :: [String] -> [String] -> String -> String replaceBasedIdxMultiple findStrList replaceStrList myText = replace()???
This function should replace each element of findStrList in myTxt with the corresponding element from the replaceStrList, so 1. with 1., 2. with 2. and so on...
Example:
replaceBasedIdxMultiple ["A","X","G"] ["N","Y","K"] "ABXMG" "NBYMK"
Could you help me with this? some tips and hints, how to begin with it?
I'm really disparate :-(
Thanks a lot in advance
Kind greetings!
解决方案replace exists in
Data.List.Utils
, part ofMissingH
package.Actually, it's a really concise implementation:
replace :: Eq a => [a] -> [a] -> [a] -> [a] replace old new = join new . split old
这篇关于Haskell - 是否存在替换函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!