在GHCI中,类型id是:
Prelude> :t id
id :: a -> a
Run Code Online (Sandbox Code Playgroud)
但是,如果我定义自己的id函数,为什么是类型变量的名称t?有没有之间的差异t和a?
Prelude> let identity x = x
Prelude> :t identity
identity :: t -> t
Run Code Online (Sandbox Code Playgroud)
a和之间没有区别t,它们被称为类型变量,代表您可以拥有的任何类型.请注意,它们是用小写字母书写的,其中类型在开头用大写字母书写(除了具有特殊语法的列表).
另外,如果您编写一个文件并将其加载到ghci中 ghci testmodule.hs
module Testmodule where
identity :: b -> b
identity x = x
Run Code Online (Sandbox Code Playgroud)
然后ghci将向您显示您在定义中使用的字母.