haskell - 将函数应用于列表的所有元素并根据函数的返回类型返回一个新列表
问题描述
我想通过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
会给你一个Bool
s列表。如果你想知道它们是否都是真的,那么你需要使用all
,或者将结果包装在and
.- 除非
Vector
是某些奇怪函数类型的别名,否则您的意思可能是distance x y
ordistance (x,y)
而不是distance (x y)
.
推荐阅读
- javascript - React Firebase Snapshot 检索所有子项
- php - PHP将日期转换为不同的格式
- javascript - 如何在 React 中获取特定映射文件的“名称”属性?
- python - 如何在 Databricks 中设置 python 测试
- java - 如何将我的 Flutter App 连接到我的 Java WebApp?
- javascript - 为什么我的动画突然结束不和谐?
- python - 出现一定次数后删除重复项
- dart - Dart (Flutter) 地图
.map 没有 toList() - python - 为分层 DataFrame 列创建数字标识符
- json - 如何在 Vue.js 中正确显示 JSON 数据?