在haskell中,我具有此功能,效果很好:
primesAtMost :: Integral a => a -> [a]
primesAtMost n = sieve [2..n]
where
sieve :: Integral a => [a] -> [a]
sieve [] = []
sieve (x:xs) = (x:sieve (filterMultiples x xs))
问题是,它的参数类型是a,并且不允许更改它。 当我尝试使用类型为Int的参数调用此函数时,会出现错误:无法将类型“ a1”与“ Int”匹配 例如:
primesAtMost (length n)