为什么第一种情况可以正常但不是第二种情况?我相信 。运算符链接函数,因为f返回布尔值我可以“不”它,但为什么它在第二种情况下不起作用?
*Main> let filterNot f xs = filter (not . f) xs
*Main> let filterNot f xs = filter (not (f)) xs
答案 0 :(得分:9)
这是因为not . f
和not f
之间存在差异。如果你想在不使用.
的情况下这样做,你将不得不使用lambda:
filterNot f xs = filter (\x -> not (f x)) xs
但这恰恰是.
!
(.) :: (b -> c) -> (a -> b) -> (a -> c)
f . g = \x -> f (g x)
仅仅因为f
返回Bool
并不意味着你可以not
它。您只能not
Bool
本身,而不是返回一个的函数。 a -> Bool
和Bool
之间存在很大差异。 not
的定义是
not :: Bool -> Bool
not True = False
not False = True
因此,如果f
本身不是True
或False
,那么您就无法将not
应用于此。
(哇,这个解释中没有否定)