![](/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.