繁体   English   中英

Haskell - 是否存在替换函数?

[英]Haskell - Does a replace function exist?

我必须制作三个函数来替换扁平字符串和列表。

我不知道,是否有像其他语言一样的替换功能。 我搜索了但不幸的是没有成功:-(

所以我的尝试还很薄。

第一功能:

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  

第二功能:

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

这个函数应该替换myTxt中的第一个findStr和replaceStrList的第一个元素,第二个findStr和第二个元素等等......

例:

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

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

你能告诉我,我的错误在哪里吗? 我怎么能修改第一个或第二个函数来获得第三个函数呢?

第三功能:

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

这个函数应该用来自replaceStrList的相应元素替换myTxt中findStrList的每个元素,所以1.用1.,2。用2.依此类推......

例:

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

你能帮帮我吗? 一些提示和提示,如何开始呢?

我真是不同:-(

非常感谢提前

亲切的问候!

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

实际上,这是一个非常简洁的实现:

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

首先, 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 (顺便说一下:在Haskell中使用素数'而不是下划线来命名函数的“局部变体/助手”,所以你宁可称之为replace' 。)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM