【发布时间】:2011-10-22 17:50:44
【问题描述】:
我对 Haskell 很陌生,才刚刚开始学习它。 我正在使用“Learn You a Haskell for Great Good!”入门教程,并看到解决“3n+1”问题的示例:
chain :: (Integral a) => a -> [a]
chain 1 = [1]
chain n
| even n = n:chain (n `div` 2)
| odd n = n:chain (n*3 + 1)
numLongChains :: Int
numLongChains = length (filter isLong (map chain [1..100]))
where isLong xs = length xs > 15
所以,numLongChains 计算所有长度为 15 步的链,对于从 1 到 100 的所有数字。
现在,我想要自己的:
numLongChains' :: [Int]
numLongChains' = filter isLong (map chain [1..100])
where isLong xs = length xs > 15
所以现在,我不想计算这些链,而是返回带有这些链的过滤列表。 但是现在编译时出现错误:
Couldn't match expected type `Int' with actual type `[a0]' Expected type: Int -> Bool Actual type: [a0] -> Bool In the first argument of `filter', namely `isLong' In the expression: filter isLong (map chain [1 .. 100])
可能是什么问题?
【问题讨论】:
标签: list haskell nested-lists