簡體   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