无法匹配预期类型'整数 - > t'与实际类型

时间:2017-10-25 03:40:45

标签: haskell functional-programming

我正在尝试运行此教科书示例,但我一直收到此错误:

* 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

1 个答案:

答案 0 :(得分:4)

您声明的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)