对列表的所有元素应用函数并根据函数的返回类型返回一个新列表
我想通过map向量列表上的 a应用 lambda 函数,并能够从结果中获取布尔值列表,然后比较布尔值列表中的所有元素
lambda = ( list x -> distance (x (5,5)) < 10)
[(0,1),(1,6),(15,36)] ->
在每个元素上应用 lambda,这将给出 :[True, True, False]
然后检查是否所有元素都是True
我试图这样做
checkConvergence :: [Vector] -> Vector -> Bool
checkConvergence list y = map ( list x -> distance (x y) < 10) list
但我得到了这个:
Couldn't match expected type ‘Bool’ with actual type [(Vector -> Vector) -> Bool]
回答
三个问题:
- 你想要
x ->而不是list x ->. map会给你一个Bools列表。如果你想知道它们是否都是真的,那么你需要要么使用all,要么将结果包装在and.- 除非
Vector是某些奇怪函数类型的别名,否则您可能指的是distance x y或distance (x,y)而不是distance (x y).