Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有可能有一个完全类型不明确的函数吗?该函数是否具有如下类型签名:
Poly :: a -> a
其中 a 是一个类型变量,例如与类型构造函数声明或类型类要求一起使用的语法?
data TypeConstructor a = One | Two a Func :: Num a => a -> a
是否有可能制作一个无处不在的id函数,它总是返回它自己的值,而不必知道正在使用什么值构造函数?
id
id :: a -> a
就像其他人所说的那样,如果 Haskell 函数不使用底层类型的任何具体特性,则默认情况下它们会自动多态。如果您打开ghci并输入:
ghci
>>> let f x = x
...然后问它的类型f,它会自动推断它f是完全多态的:
f
>>> :type f f :: t -> t
如果你使用文件也是一样的。你可以定义:
f x = x
...并且编译器将推断出f具有 type a -> a。你也可以显式地注解f:
a -> a
f :: a -> a f x = x