如何在字符串中找到某个字符并用空格代替它,在该字符串中使用匿名函数在Haskell中留下
我需要使用 foldl 来编写一个函数 并返回一个匿名功能。匿名函数会接收一个字符“ C”,并与“ str”中的“ c”的每个实例与字符串“ str”示例中的剩余数量的字符
speak :: String -> (Char -> String)
示例:
“ hello”''e' - > “ h3llo”
“ gate”'t' - > “ GA1E”
我尝试了此代码,但无法使其正常工作:
speak :: String -> (Char ->String)
speak str = foldl (\x -> if x == str then x = show(length str) else str) str
I need to write a function ,with the use of foldl, which recieves a string "str"
and returns an anonymous function. The anonymous functions receives a char 'c' and exchanges every instance of 'c' in "str" with the remaining number of chars in the string "str"
speak :: String -> (Char -> String)
example:
"Hello" 'e' -> "H3llo"
"gate" 't' -> "ga1e"
I've tried this code, but cant get it to work properly:
speak :: String -> (Char ->String)
speak str = foldl (\x -> if x == str then x = show(length str) else str) str
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法将值分配给
x
您需要做的是返回show(长度xs)++ xs
如果字符x
与您要查找的一个或x:xs
(因此,如果它确实确实如此不匹配。您的speak
还将char
作为第一个参数,然后将字符串
转换为string
,所以:或交换参数:
You can not assign a value to
x
What you need to do is either returnshow (length xs) ++ xs
in case the characterx
is the same as the one you are looking for, orx:xs
(so a normal prepend ofx
toxs
) in case it does not match. Yourspeak
also has aChar
as first parameter, and then converts aString
to aString
, so:or with swapped parameters: