【发布时间】:2015-01-16 15:26:37
【问题描述】:
我需要一个检查列表条件的函数。
例如:
countP :: [a] -> (a -> Bool) -> Int
输入:
countP [1,-2,0,-1,5] (>0)
应该返回 2,因为有两个大于零的数字。
这是我到目前为止所做的:
countP :: [a] -> (a -> Bool) -> Int
countP [] _ = []
countP (x:xs) condition = if condition x then 1:countP xs condition else countP xs condition
它返回 [1,1] 而不是数字二。它必须是递归的。
我该怎么做?
【问题讨论】: