简体   繁体   English

无法将预期的类型'Integer-> t'与实际类型匹配

[英]Couldn't match expected type 'Integer -> t' with actual type

I'm trying to run this textbook example but I keep getting this error: 我正在尝试运行此教科书示例,但始终出现此错误:

* 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)

I don't understand why I'm getting this error. 我不明白为什么会收到此错误。 I'm inputting the correct types. 我输入的是正确的类型。
My code is: 我的代码是:

type Name = String 
type Age = Int
data People = Person Name Age

showPerson :: People -> String
showPerson (Person a b) = a ++ " -- " ++ show b

Your showPerson function, as declared, has only one argument. 声明的showPerson函数只有一个参数。 This argument is of type People . 此参数是People类型的。

However, judging by the error you're quoting, you are trying to call this function with two arguments - "charles" and 18 . 但是,从引用的错误来看,您试图使用两个参数调用此函数- "charles"18 The first argument is of type String , second - of type Int . 第一个参数的类型为String ,第二个参数的类型为Int

This is what the compiler is trying to tell you when it says: 这是编译器试图告诉您的内容:

The function `showPerson' is applied to two arguments, 
but its type `People -> [Char]' has only one

To correctly call your function, you need to first create a value of type People , then pass that value to the function as argument: 要正确调用函数,您需要首先创建一个People类型的值,然后将该值作为参数传递给该函数:

p = Person "charles" 18
showPerson p

Or the same thing in one line: 还是同一行:

showPerson (Person "charles" 18)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM