List Haskell-是否存在替换函数?
我必须做三个函数来替换平面字符串和列表中的字符串 我不知道,是否有像其他语言一样的替换函数。但不幸的是,我没有成功:-( 所以我的尝试还很薄弱 第一个功能:List Haskell-是否存在替换函数?,list,haskell,nested,replace,List,Haskell,Nested,Replace,我必须做三个函数来替换平面字符串和列表中的字符串 我不知道,是否有像其他语言一样的替换函数。但不幸的是,我没有成功:-( 所以我的尝试还很薄弱 第一个功能: replace :: String -> String -> String -> String replace findStr replaceStr myText = replace()?? replace :: String -> String -> String -> String
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()???
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()???
我的第一个函数方法:
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()???
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()???
第二个功能:
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()???
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中的第一个findStr,用第二个元素替换第二个findStr,依此类推
示例:
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()???
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()???
第二个功能的我的方法:
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()???
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()???
我现在非常接近最终结果,但是计数器没有增加。
你能告诉我,我的错在哪里吗?
我如何修改第一个或第二个函数来获得第三个函数呢
第三个功能:
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()???
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"
你能帮我吗?一些提示和提示,如何开始
我真的完全不同:-(
提前多谢
亲切的问候!首先,
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
s列表上递归,而不需要一直按原样传递它
因此,您需要做的是实现locateSub
。使用isPrefixOf
您已经走上了正确的道路。实际上,它看起来很像您的\u replace
(顺便说一句:Haskell的客户习惯是使用prime”
,而不是用下划线来命名“本地变体/助手”函数的一部分,因此您更愿意将其称为replace'
)存在于Data.List.Utils
中,它是MissingH
包的一部分
实际上,这是一个非常简洁的实现:
replace :: Eq a => [a] -> [a] -> [a] -> [a]
replace old new = join new . split old
如果要替换单个字符,则可以将
replace
编写为map
。我没有看到任何“尝试”,只输入签名。你真的试过写其中一个吗?没那么难…@leftaroundabout我对Haskell完全陌生,我不知道如何在Haskell中替换。如果它是像C#或Java之类的命令式语言,它会很简单,但在Haskell中对我来说一切都很困难。请帮助,这样我可以通过明天的考试:-(@larsmans你什么意思beimap.-
?你能给我举个例子吗?对于我找到这段代码的第一个函数,我不完全理解它,但我会尝试理解它。但是它只适用于纯文本而不适用于列表。我如何修改它以使它也适用于列表?它不是太长,太复杂吗太复杂了??如果你的考试是明天,而你甚至不知道如何开始考试,那可能就太晚了。首先非常感谢你的帮助!现在我尝试使用你的代码,我发现一个编译错误不在范围内:`locateSub'
。代码有什么问题吗?没有,正如我说的:你需要做的是实现locateSub
。好的,我试过了,但它根本不起作用。locateSub:(Eq a)=>[a]->[a]->[a]->可能([a],[a])locateSub oldSub newSub list=map(=oldSub)(tails list)
。我一点也不明白:-(你能帮我解决这个问题吗?我明天就开始学习你的代码…locateSub
不接受newSub
参数。使用tails
原则上是个好主意,但在这里不起作用,因为它“扔掉了”左边是您需要的。我认为在这种情况下,您最好使用手动递归。如果您甚至不能读取这样一个简单的类型签名,您就真的有麻烦了。提示:您可以将其实例化为locateSub::String->String->Maybe(String,String)
。当我试图访问此库时,导入数据.List.Utils
。它会抛出一个错误,无法找到模块`Data.List.Utils'
。我如何使其可访问?这意味着什么?我应该安装一些东西还是代码?如何使用它,如果是代码?Cabal是安装Haskell软件包的工具,请阅读维基:好吧,很遗憾,我不能使用它,因为我必须使用标准的环境,我们在学校里有-但无论如何,非常感谢你的提示,你仍然可以查看它的来源(甚至窃取整个东西)。