[英]How to use curried functions in Haskell with map?
通过重新定义前奏函数尝试学习如何使用折叠:
import Prelude hiding (sum, product, length, and, or, all, any, filter)
到目前为止,我已经做好了所有工作的准备,但是我无法弄清楚我在做什么方面都做错了。 我将其定义如下:
and :: [Bool] -> Bool
and = foldr (&&) True
...
all :: (a -> Bool) -> [a] -> Bool
all = and $ map
但这会显示一条错误消息:
Probable cause: ‘map’ is applied to too few arguments
我也尝试将其定义为:
and :: [Bool] -> Bool
and = foldr (&&) True
...
all :: (a -> Bool) -> [a] -> Bool
all f [xs] = and $ map f [xs]
这可以编译,但是当我尝试调用它时说:
[1 of 1] Compiling Fold ( Fold.hs, interpreted )
Ok, modules loaded: Fold.
*Fold> all even [0,2,4,6]
*** Exception: Fold.hs:17:1-29: Non-exhaustive patterns in function all
我不明白,因为[xs]不应该匹配任何列表,甚至是空列表吗? 为什么在不包含列表但不包含地图的情况下咖喱文件夹? 任何帮助,将不胜感激。
您将功能应用程序与组成混合在一起。 这有帮助吗?
all :: (a -> Bool) -> [a] -> Bool
all fn = and . (map fn)
实际上,这等同于您的代码+ @Carcigenicate的注释
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.