Haskell函数类型的说明 [英] Haskell function type clarification

查看:151
本文介绍了Haskell函数类型的说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解函数的Haskell类型变量。我写了一个函数:

  applyTwice fx = f(fx)

我试着理解这个函数的类型变量,所以我做了一个:t applyTwice 。这就是Haskell解释类型的方式:

  applyTwice ::(t  - > t) - > t  - > t 

然后我创建了另一个函数:

 applyOnce fx = fx 

这次为:t Haskell返回

  applyOnce ::(t1  - > t) - > t1  - > t 

我的问题是


  1. 我们如何阅读/理解这些函数返回并返回的内容? 如果他们说任何左边的 - > 是该函数的内容,右边是它的返回值,那么它应该不是
    <$ c $ (t - > t) - > t) - >吨?对于(fx)((t - > c)来说,(t - > t)对于 f(fx)和返回类型 t >。 为什么函数的类型解释为 applyOnce ::(t1 - > t) - > t1 - >吨?因为我们只接受一个函数并返回它的值。如果它不是 applyOnce ::(t1 - > t) - > t1


作为Haskell的初学者,我希望在此有任何建议。

解决方案

我们有:

  applyTwice fx = f(fx)

只有 x fx 是等价的(否则你将如何传递 fx 转换为一个函数,它需要 x ?)。因此,可以说 applyTwice 需要2个输入参数:


  • 使用类型t并返回相同的类型t

  • 某种类型的输入



现在这个输入可以是任何类型,比如t1。但是你也必须能够把这个函数应用到这个输入中,所以输入的类型也必须是t。放在一起,我们得到签名:

  applyTwice ::(t  - > t) - > t  - > t 

请记住,只有最后一项是返回类型,其他所有项都是输入类型。



现在考虑:

  applyOnce fx = fx 

简单地说,关于函数f的所有内容都表示它应该能够接受任何类型的输入x。除了, 应用程序应该也返回相同类型。因此,我们结束了您看到的签名:

  applyOnce ::(t1  - > t) - > t1  - > t 

其中t1可以是任何类型,t可以是任何类型(与t1相同或不同),但要求返回类型 f 匹配返回类型 applyOnce ,这里用t表示。



TL; DR:具有n个输入的函数的函数签名总是有(n + 1)个项,最后一项将是函数的返回类型。

然而,你应该注意到,事实并非如此。 Haskell中的所有函数实际上都带有一个参数,即它们是 curried 。您可以在这里阅读更多关于 的内容。


I am trying to understand Haskell type variables for functions. I wrote a function:

applyTwice f x = f(f x)

I tried to understand the type variables for this function, so I did a :t applyTwice. This is how Haskell interprets the types:

applyTwice :: (t -> t) -> t -> t

Then I created another function:

applyOnce f x = f x

This time for :t Haskell returns

applyOnce :: (t1 -> t) -> t1 -> t

My questions are

  1. How do we read/understand what these functions take and return?

  2. This is for applyTwice. If they say anything left side of the -> is what the function takes and right hand side is what it returns then should it not be applyTwice :: ((t -> t) -> t) -> t? (t -> t) for (f x) and ((t -> t) -> t) for f (f x) and the return type being t.

  3. This is for applyOnce. Why is the function's type interpreted as applyOnce :: (t1 -> t) -> t1 -> t? because we only take a function and return it's value. Should it not be applyOnce :: (t1 -> t) -> t1?

As a beginner in Haskell I would like any advice in this.

解决方案

We have:

applyTwice f x = f(f x)

This would make sense only if the types of x and f x were equivalent (else how would you be able to pass f x to a function that takes x?). Therefore, you could say that applyTwice takes 2 input arguments:

  • A function that takes a type t and returns the same type t
  • An input of some type

Now this input could be of any type, say t1. But you also have to be able to apply the function to this input, and so the type of the input must also be t. Put together, we get the signature:

applyTwice :: (t -> t) -> t -> t

Remember that only the last term is the return type, and all others are types of the inputs.

Now consider:

applyOnce f x = f x

Simplistically, all it says about the function f is that it should be able to accept an input x of any type. There is nothing about what type the function should return except that applyOnce should also return that same type. Hence, we end up with the signature you see:

applyOnce :: (t1 -> t) -> t1 -> t

where t1 could be any type and t could be any type (same or different from t1), but requiring that the return type of f matches the return type of applyOnce, represented by t here.

TL;DR: Your function signature for a function with n inputs will always have (n+1) terms, and the last term will be the return type of the function.

However, you should note that this is not actually how things work under the hood. All functions in Haskell actually take one argument i.e. they are curried. You can read more about this here.

这篇关于Haskell函数类型的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆