首页 > 解决方案 > 将函数应用于列表的所有元素并根据函数的返回类型返回一个新列表

问题描述

我想通过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]

标签: haskelllambdatype-mismatchmap-function

解决方案


三个问题:

  1. 你想要\ x ->而不是\ list x ->.
  2. map会给你一个Bools列表。如果你想知道它们是否都是真的,那么你需要使用all,或者将结果包装在and.
  3. 除非Vector是某些奇怪函数类型的别名,否则您的意思可能是distance x yordistance (x,y)而不是distance (x y).

推荐阅读