繁体   English   中英

对列表的所有元素应用 function 并根据函数的返回类型返回一个新列表

[英]Apply function on all elements of list and return a new list based on function's return type

我想通过map在向量列表上应用 lambda function 并能够从结果中获取布尔列表,然后比较布尔列表中的所有元素

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]

三个问题:

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM