简体   繁体   English

无法将预期类型“Bool”与实际类型“Char -> Bool”匹配

[英]Couldn't match expected type ‘Bool’ with actual type ‘Char -> Bool´

Hi I'm trying to filter a string so it only outputs the letters in it.嗨,我正在尝试过滤一个字符串,因此它只输出其中的字母。

This is what I have so far这是我到目前为止

filteredText :: String -> String
filteredText (x:xs) = filter (\toLower x -> x `elem` ['a'..'z']) xs

and gives me this error:并给我这个错误:

Couldn't match expected type ‘Bool’
with actual type ‘Char -> Bool’

I believe what you want is filteredText xs = filter (\\x -> (toLower x) `elem` ['a'..'z']) .我相信你想要的是filteredText xs = filter (\\x -> (toLower x) `elem` ['a'..'z'])

Between \\ and -> are the formal arguments to anonymous functions. \\->是匿名函数的正式参数。 You had \\toLower x ->... , meaning two arguments where one is expected:你有\\toLower x ->... ,意思是两个参数,其中一个是预期的:

filter :: (a -> Bool) -> [a] -> [a] -- takes f :: (a -> Bool), as :: [a], gives [a]

f only takes one argument. f 只接受一个参数。

Also, filteredText (x:xs) pattern matches x as the head of the list (the first element) and xs the tail (the rest of the list but the head).此外, filteredText (x:xs)模式匹配 x 作为列表的头部(第一个元素)和 xs 尾部(列表的其余部分,但头部除外)。 Therefore, the first element was being ignored.因此,第一个元素被忽略了。 I assumed it was no intentional.我以为不是故意的。

The first parameter of filter should be a function that when called for an element determine whether we wish to keep that element in the result or not. filter的第一个参数应该是一个函数,当调用一个元素时,它决定我们是否希望将该元素保留在结果中。 This is thus a function that for a given x will return True or False , and thus has as shape \\x -> … .因此,这是一个函数,对于给定的x将返回TrueFalse ,因此形状为\\x -> …

Another problem is that you match on the (x:xs) pattern.另一个问题是您匹配(x:xs)模式。 This means that your expression would omit the first item of the list, and only will process the tail of the list.这意味着您的表达式将省略列表的第一项,而只会处理列表的尾部。 Furthermore this means that passing an empty string, would raise an error.此外,这意味着传递空字符串会引发错误。 The entire list can thus be named xs :因此整个列表可以命名为xs

filteredText :: String -> String
filteredText xs = filter (\x -> …) xs

Here you thus need to implement the expression: you first use toLower to obtain the lowercase variant of the given character, and then you can use `elem` ['a' .. 'z'] to check if the the lowercase of x belongs to that list.这里你需要实现表达式:你首先使用toLower来获取给定字符的小写变体,然后你可以使用`elem` ['a' .. 'z']来检查x的小写属于那个名单。 I leave the implementation as an exercise.我将实现作为练习。

You can get something very close to what you wrote, with你可以得到一些非常接近你写的东西,用

> filter ( \ (toLower -> x) -> x `elem` ['a'..'z'] ) "1243sdfwerweKJHKJ(*&@*#9798"
=> "sdfwerweKJHKJ"

You will need to enable ViewPatterns for that.您需要为此启用 ViewPatterns。 The GHCi command is :set -XViewPatterns . GHCi 命令是:set -XViewPatterns

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将预期的类型'(Char,Bool)-> Bool'与实际类型'Char'相匹配 - Couldn't match expected type ‘(Char, Bool) -> Bool’ with actual type ‘Char’ 无法将预期类型`Bool'与实际类型`IO Bool'匹配 - Couldn't match expected type `Bool' with actual type `IO Bool' 无法将预期类型`Bool'与实际类型`Card - > Bool'匹配 - Couldn't match expected type `Bool' with actual type `Card -> Bool' 无法将预期类型“Bool”与实际类型“a -> Bool”匹配 - Couldn't match expected type ‘Bool’ with actual type ‘a -> Bool’ 无法将预期类型'Integer - > t'与实际类型'Bool'匹配 - Couldn't match expected type ‘Integer -> t’ with actual type ‘Bool’ 无法将预期类型“Bool”与实际类型“[[Integer]] 匹配 - Couldn't match expected type `Bool' with actual type `[[Integer]] 无法将预期类型“布尔”与实际类型“ Int”匹配 - Couldn't match expected type ‘Bool’ with actual type ‘Int’ haskell 无法将预期类型与实际类型“Bool”匹配 - haskell couldn't match expected type with actual type 'Bool' 无法将预期的类型“布尔”与实际类型“(a,a)”匹配 - Couldn't match expected type ‘Bool’ with actual type ‘(a, a)’ 无法将预期类型“[Integer]”与实际类型“Bool”匹配 - Couldn't match expected type `[Integer]' with actual type `Bool'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM