kil*_*tek 1 haskell types filter
有没有办法在字符串上使用过滤器功能,这样:
filter (=="!") "!!some!!_!!string!!"
Run Code Online (Sandbox Code Playgroud)
应该输出"some_string"(上面的情况).现在我得到的是一个类型错误:
Couldn't match expected type `[Char]' against inferred type `Char'
Run Code Online (Sandbox Code Playgroud)
如果我将第二个过滤器参数类型更改为["!! some !! _ !! string !!"],则类型错误消失,但只输出一个空列表.(不完全是我想要的)
我认为字符串是列表但很明显"!! !! !! !! !! !!" 不被视为列表而是作为字符.
有人提示吗?
ken*_*ytm 10
Prelude> filter (/='!') "!!some!!_!!string!!"
"some_string"
Run Code Online (Sandbox Code Playgroud)
类型filter是(a -> Bool) -> [a] -> [a].由于第二个参数是String = [Char],意思是[a]= String = [Char],我们推断a必须是Char.因此,该函数必须将Char作为输入.因此,您需要使用'!',而不是"!".