如何修复替换Haskell功能?
它可以工作时:替换:: eq a => a - > a - > [a] - > [a]会。如何将AZ A转换为代码中的[A]?
replace :: Eq a => a -> [a] -> [a] -> [a]
replace _ _ [] = []
replace a x (y:ys)
| a == y = x : replace a x ys
| otherwise = y : replace a x ys
Example:
replace '?' "a" "" == ""
replace 'a' "e" "alma" == "elme"
replace 'a' "e" "nincsbenne" == "nincsbenne"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用错误的操作员作为第一个后卫(
a == y
) -:
用于将头部元素预先列入列表,但x
是一个列表不是一个元素,因此您需要使用++
加入两个列表(X
,另一个由递归调用返回):相关 - haskell(:)和(++)差异
You are using wrong operator for the first guard (
a == y
) -:
is used to prepend a head element to a list butx
is a list not a single element, so you need to use++
which concatenates two lists (x
and one returned by recursive call):Related - Haskell (:) and (++) differences