遍历一个字符串并用haskell中的子字符串替换单个字符 [英] Iterating through a String and replacing single chars with substrings in haskell
问题描述
当前项目时遇到了一些问题。这个想法是,我必须通过一个字符串并用新的子字符串替换某些字符
。例如,如果我有一个字符串FLXF,我想用一个名为FLF的子字符串替换每个F
,结果应该是FLFLXFLF。现在我一直在研究这个
特定的问题几个小时。我一直在阅读类型,可能派上用场的不同功能(地图,折叠等),但我一直无法解决这个问题。
下面的代码是我有过的一些不同的尝试:
apply :: String - >字符串
apply [] = []
apply(x:xs)= if(x =='F')
然后显示Hello
apply xs
else(apply(xs))
这个例子在这里我只是试图展示你好,遇到'F',但它显示的是,所以这显然不起作用。我真的不确定是否有其他陈述是这里的方法。我也在想功能图可能会做到这一点。这里我想到的代码可能看起来像这样:
map(\xy - > if y ==' F'然后是FLDelse y)FLF
但是这给我一个类型错误。所以你可以看到我迷路了。请原谅我对Haskell的可怜知识,但我仍然对它不熟悉。我真的希望你们中的一些人能够帮助我,或者让我朝着正确的方向前进。如果我对某些事情不清楚,请随时提问。
预先感谢您!
John
map(\xy - > if y =='F'then FLDelse y)FLF
这几乎是正确的。
第一个...为什么函数需要两个参数?
map(\y - > ;如果y =='F',那么FLDelse y)FLF
其余类型错误是因为然后
分支给出了 String
,但是 else
分支给出了一个 Char
(两个分支都必须给出相同类型的值)。因此,我们将使 else
分支给出 String
来代替(回想一下 String
是
[Char]
)的同义词:
map(\y - > if y =='F'thenFLDelse [y])FLF
现在问题是这给你一个 [String]
值而不是 String
。所以我们将所有这些字符串连接在一起:
concat(map(\y - > if y =='F '然后是FLDelse [y])FLF)
concat
和 map
已经足够普遍,以至于有一个标准函数可以将它们结合在一起。
concatMap(\y - > if y =='F'thenFLDelse [y])FLF
I am trying to learn some Haskell and I find it difficult. I am having some issues with my current project. The idea is that I have to go through a String and substitute certain chars with new substrings. For instance if I have a String "FLXF" and I want to replace every F with a substring called "FLF" the result should be "FLFLXFLF". Now I have been working on this specific problem for hours. I have been reading up on types, different functions that might come in handy (map, fold, etc) and yet I have not been able to solve this problem.
The code below is some of the different tries I have had:
apply :: String -> String
apply [] = []
apply (x:xs) = if (x == 'F')
then do show "Hello"
apply xs
else (apply (xs))
This example here I was just trying to show hello every time I encountered a 'F', but all it shows is "", so this clearly does not work. I am really not sure an if else statement is the way to go here. I was also thinking the function map might do the trick. Here the code I was thinking about could look something like this:
map (\x y -> if y == 'F' then "FLD" else y) "FLF"
but that gives me a type error. So as you can see I am lost. Excuse me my poor knowledge to Haskell, but I am still new to it. I really hope some of you can help me out here or give me a push in the right direction. Feel free to ask questions if I have been unclear about something.
Thank you in advance!
John
map (\x y -> if y == 'F' then "FLD" else y) "FLF"
This is nearly right.
First... why does the function take two arguments?
map (\y -> if y == 'F' then "FLD" else y) "FLF"
The remaining type error is because the then
branch gives a String
, but the else
branch gives a Char
(the two branches must each give a value of the same type). So we'll make the else
branch give a String
instead (recall that String
is a synonym for [Char]
):
map (\y -> if y == 'F' then "FLD" else [y]) "FLF"
Now the problem is that this gives you a [String]
value instead of a String
. So we'll concatenate all those strings together:
concat (map (\y -> if y == 'F' then "FLD" else [y]) "FLF")
This combination of concat
and map
is common enough that there's a standard function that combines them.
concatMap (\y -> if y == 'F' then "FLD" else [y]) "FLF"
这篇关于遍历一个字符串并用haskell中的子字符串替换单个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!