【发布时间】:2021-04-08 21:45:41
【问题描述】:
我是 Haskell 的新手,我对语法不太熟悉。我基本上是在尝试从 Haskell 编写我自己的 findIndices 版本。这是我目前所拥有的:
findIndices :: (a -> Bool) -> [a] -> [Int]
findIndices pred [] = []
findIndices pred (x:xs) = [n | n <- [0..length xs], pred x == True]
我被困在列表理解部分。我希望能够询问列表的所有元素是否 pred x == True,但这只会在列表的头部询问。有什么方法可以递归整个列表来询问 pred x == True 吗?
【问题讨论】:
-
x是列表的第一个元素,xs是其余元素,因此pred x将始终为True或始终为False,无论@ 的值如何987654327@. -
使用
expr == True与仅使用expr完全相同,因为无论如何结果都是Bool类型
标签: haskell functional-programming