简体   繁体   中英

Haskell sum a list of integers

lately I've been trying to learn Haskell and now I'm stuck at the following problem:

-- sumList

sumList :: List -> Integer

sumList l = sum l

Now I get the following error message in VS Code:

{
    "resource": "/home/santino/Studium_Informatik/Studium_Informatik/UnitTest/unittest.hs",
    "owner": "Haskell0",
    "code": "-Wdeferred-type-errors",
    "severity": 8,
    "message": "• Couldn't match type ‘List’ with ‘t0 Integer’\n  Expected type: List -> Integer\n    Actual type: t0 Integer -> Integer\n• In the expression: sum\n  In an equation for ‘sumList’: sumList = sum",
    "source": "typecheck",
    "startLineNumber": 22,
    "startColumn": 11,
    "endLineNumber": 22,
    "endColumn": 14
}

I have tried googeling the question, but I only get results from Integer to Integer and not from List to Integer. Thanks in advance!

sumList :: [Integer] -> Integer
sumList l = sum l

As the error message suggest you need to supply a list of numbers to sum . Here's the signature of sum :

λ> :t sum
sum :: (Foldable t, Num a) => t a -> a

It takes a Foldable of Num s and returns a Num .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM