想象一下,我们有一个列表,我们只想从列表中取出正方形数
isSquare :: Int -> Bool
isSquare n = truncate(sqrt(x)) * truncate(sqrt(x)) == n
where x = fromIntegral n
squareTriSemi = filter (isSquare) triSeries
这里是triSeries的定义
triSeries 0 = [0]
triSeries n = map triangular $take n $iterate (+1) 1
where triangular x = x * (x + 1) `div` 2
但它没有编译它像这样抱怨
Couldn't match expected type `[Int]' with actual type `Int -> [t0]'
In the second argument of `filter', namely `triSeries'
In the expression: filter (isSquare) triSeries
In an equation for `squareTriSemi':
squareTriSemi = filter (isSquare) triSeries
任何帮助?
答案 0 :(得分:4)
您对triSeries
的定义需要Int
并返回一个列表。也就是说,triSeries
本身不是列表,但像triSeries 10
这样的表达式是列表。
修复squareTriSemi
定义的一种方法是添加一个传递给triSeries
的参数:
squareTriSeries n = filter isSquare (triSeries n)