簡體   English   中英

Haskell 對整數列表求和

[英]Haskell sum a list of integers

最近我一直在嘗試學習 Haskell,現在我遇到了以下問題:

-- sumList

sumList :: List -> Integer

sumList l = sum l

現在我在 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
}

我試過用谷歌搜索這個問題,但我只能得到從整數到整數的結果,而不是從列表到整數。 提前致謝!

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

由於錯誤消息表明您需要提供要sum的數字列表。 這是sum的簽名:

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

它需要一個NumFoldable並返回一個Num

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM