![](/img/trans.png)
[英]Haskell Couldn't match expected type `Integer' with actual type `[Integer]'
[英]Couldn't match expected type 'Integer -> t' with actual type
我正在嘗試運行此教科書示例,但始終出現此錯誤:
* Couldn't match expected type `Integer -> t'
with actual type `[Char]'
* The function `showPerson' is applied to two arguments,
but its type `People -> [Char]' has only one
In the expression: showPerson "charles" 18
In an equation for `it': it = showPerson "charles" 18
* Relevant bindings include it :: t (bound at <interactive>:38:1)
我不明白為什么會收到此錯誤。 我輸入的是正確的類型。
我的代碼是:
type Name = String
type Age = Int
data People = Person Name Age
showPerson :: People -> String
showPerson (Person a b) = a ++ " -- " ++ show b
聲明的showPerson
函數只有一個參數。 此參數是People
類型的。
但是,從引用的錯誤來看,您試圖使用兩個參數調用此函數- "charles"
和18
。 第一個參數的類型為String
,第二個參數的類型為Int
。
這是編譯器試圖告訴您的內容:
The function `showPerson' is applied to two arguments,
but its type `People -> [Char]' has only one
要正確調用函數,您需要首先創建一個People
類型的值,然后將該值作為參數傳遞給該函數:
p = Person "charles" 18
showPerson p
還是同一行:
showPerson (Person "charles" 18)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.