Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell类型类错误_Haskell - Fatal编程技术网

Haskell类型类错误

Haskell类型类错误,haskell,Haskell,我搞不懂,代码: smallSum :: (Ord a, Integral a) => a -> a smallSum n | n < 0 = 0 | (n < 20) = n + smallSum (n - 1) | otherwise = error "Number needs to be in 1..10" fastSumOfSeriesLength :: (Ord a, Integral a) => a -> a fastS

我搞不懂,代码:

smallSum :: (Ord a, Integral a) => a -> a
smallSum n 
    | n < 0 = 0
    | (n < 20) = n + smallSum (n - 1)
    | otherwise = error "Number needs to be in 1..10"

fastSumOfSeriesLength :: (Ord a, Integral a) => a -> a
fastSumOfSeriesLength x 
    | x < 10 = smallSum x
    | x >= 10 = sum (take (rest - 1) [dif !! (firstDigit - 1), dif !! (firstDigit - 1) + 100..]) + smallList !! (firstDigit - 1)
    where
        smallList = [smallSum x | x <- [1..10]]
        largeList = [smallSum x | x <- [11..20]]
        dif = [l - s | l <- largeList, s <- smallList]
        firstDigit = x `mod` 10
        rest = x `div` 10

我期待有人指出什么是错误的,它会看起来轻工作,我需要谷歌了解更多关于这个错误

查看
(!!)
获取的类型:

*Main> :t (!!)
(!!) :: [a] -> Int -> a
*Main> :t take
take :: Int -> [a] -> [a]

由于在表达式上使用这些函数的类型与
x
相同,这意味着
x
必须是
Int
——但您声明此函数应适用于任何类型的(整数)数。(如果你慢慢读错误,希望你能看到它这么说。)最简单的修复方法是导入
数据。列出
并使用
genericIndex
genericTake

快速提示:你可以删除函数的类型声明,然后将其加载到GHCi中。使用
:t expr
命令,您可以询问函数的类型。我更愿意说,最简单的修复方法是删除类型声明或使其为Int->Int,但这取决于他是否需要多态性。@MdxBhmt可能最简单的方法是删除整个文件。但我对“简单”的标准不如我对“简单”的标准有效
*Main> :t (!!)
(!!) :: [a] -> Int -> a
*Main> :t take
take :: Int -> [a] -> [a]