哈斯克尔 - 做了替换功能存在吗?(Haskell - Does a replace functio

2019-09-02 04:48发布

我必须做出三个功能为平串,并在列表中替换。

我不知道,是否有像在其他语言中替换功能。 我搜索的结果不过可惜没有成功:-(

所以我尝试又是相当薄。

第一个功能:

replace  ::  String  ->  String  ->  String  ->  String
replace findStr replaceStr myText = replace()??

我的第一功能的方法:

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  

第2个功能:

replaceBasedIdx ::  String  ->  [String]  ->  String  ->  String
replaceBasedIdx findStr replaceStrList myText = replace()???

这个功能应该与replaceStrList,第二FINDSTR与第二个元素等的第1个要素代替第一FINDSTR在myTxt ...

例:

replaceBasedIdx   "a"  ["G","V","X"]  "Haskell is a language"
"HGskell is V lXnguage"

我的第二个功能的方法:

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

我现在非常接近最终结果,但是计数器不递增。

你能告诉我,我的错误是什么? 我怎么能modifey第一或第二个函数来取得第三个功能也?

第三个功能:

replaceBasedIdxMultiple  ::  [String]  ->  [String]  ->  String  ->  String
replaceBasedIdxMultiple  findStrList replaceStrList myText = replace()???

此函数应与来自replaceStrList相应元素替换findStrList中的每个元素在myTxt,所以1. 1.,2与2等...

例:

replaceBasedIdxMultiple ["A","X","G"] ["N","Y","K"]  "ABXMG"
"NBYMK"

你能帮助我吗? 一些技巧和提示,如何与它开始?

我真的很不同:-(

非常感谢提前

样的问候!

Answer 1:

替换存在于Data.List.Utils的一部分MissingH包。

其实,这是一个非常简洁的方式:

replace :: Eq a => [a] -> [a] -> [a] -> [a]
replace old new = join new . split old


Answer 2:

首先, join是一个不好的名字为这已经是一个标准功能 。 另外,我不知道为什么你定义了这个功能,通过这种方式 - 它似乎并没有做任何事情了许多有益的。

不过没关系,你也试试看吧。 现在让我们找到一个妥善的解决办法...

由于通常是在Haskell一个好主意,我们要打破这种成子问题。 什么是首先需要的是找到你想更换子串。 这可能看起来像

locateSub :: (Eq a) =>
        [a]             -- ^ The sought sublist.
     -> [a]             -- ^ The source list.
     -> Maybe ([a],[a]) -- ^ Everything to the left and, if found, everything
                        -- to the right of the sought sublist. No need to return
                        -- the sublist itself in between since we already know it!

使用此功能, replace是直截了当:

replace oldSub newSub list
    = case locateSub oldSub list of
        Nothing -> list   -- Sublist not found: we're done already!
        Just (l, r) -> l ++ newSub ++ replace oldSub newSub r

replaceBasedIdx是不是要困难得多,你只需要在递归的列表newSub真是让人不是总是把它当作-是。

所以,你需要做的是落实locateSub 。 随着isPrefixOf你已经在正确的轨道上。 实际上,它看起来很像你_replace (BTW:这是custumary在Haskell使用黄金'而不是下划线来命名函数的‘局部变化/帮手’,所以你宁愿把它replace' 。)



文章来源: Haskell - Does a replace function exist?