Home > Enterprise >  Haskell sum a list of integers
Haskell sum a list of integers

Time:10-21

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!

CodePudding user response:

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 Nums and returns a Num.

  • Related