简体   繁体   English

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

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

I want to apply a lambda function through a map on a list of vectors and be able to get a list of Booleans from the results and then compare all elements in the list of Booleans我想通过map在向量列表上应用 lambda function 并能够从结果中获取布尔列表,然后比较布尔列表中的所有元素

lambda = (\ list x -> distance (x (5,5)) < 10) 

[(0,1),(1,6),(15,36)] -> 

Apply the lambda on each elements, which would give: [True, True, False] and then check if all elements are True在每个元素上应用 lambda,这将给出: [True, True, False]然后检查所有元素是否为True

I tried to do this我试着这样做

checkConvergence :: [Vector] -> Vector -> Bool
checkConvergence list y = map (\ list x -> distance (x y) < 10)  list

But I got this:但我得到了这个:

 Couldn't match expected type 'Bool' with actual type [(Vector -> Vector) -> Bool]

Three problems:三个问题:

  1. You want \ x -> instead of \ list x -> .你想要\ x ->而不是\ list x ->
  2. map will give you a list of Bool s. map会给你一个Bool的列表。 If you want to know if they're all true, then you need to either use all instead, or wrap the result in and .如果您想知道它们是否都是真的,那么您需要使用all代替,或者将结果包装在and中。
  3. Unless Vector is an alias for some weird function type, you probably meant distance xy or distance (x,y) instead of distance (xy) .除非Vector是一些奇怪的 function 类型的别名,否则您的意思可能是distance xydistance (x,y)而不是distance (xy)

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

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