[英]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:三个问题:
\ x ->
instead of \ list x ->
.\ x ->
而不是\ list x ->
。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
中。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 xy
或distance (x,y)
而不是distance (xy)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.