为什么Haskell类型签名声明有多个箭头? [英] Why do Haskell type signature declarations have multiple arrows?

查看:108
本文介绍了为什么Haskell类型签名声明有多个箭头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,这句话措辞很差,但是很难描述.

Sorry this is worded poorly, but it's hard to describe.

我想我会跳到示例:

add                     :: Integer -> Integer -> Integer
add x y                 =  x + y

为什么:

:: Integer -> Integer -> Integer

代替:

:: Integer, Integer -> Integer

箭头是"函数类型映射运算符",不是某种分隔符,不是吗?

the arrow is the "Function type-mapping operator", not some kind of separator, no?

推荐答案

因为存在咖喱.考虑一下这种类型:

Because of currying. Think about the type of this:

add 3 :: Integer -> Integer

如果给add一个数字,它将返回一个将Integer映射到另一个整数的函数.因此,您可以这样做:

If you give add one number it returns a function that maps an Integer to another integer. So you could do this:

map (add 3) [1..10]

在部分应用程序中,将参数与返回类型区别对待是没有意义的.

It doesn't make sense to treat arguments differently from return types with partial application.

编辑以澄清

我认为bheklilr很好地指出了可以这样读取类型签名

I think bheklilr makes a good point that the type signature can be read like this

add :: Integer -> (Integer -> Integer)

我们可以采用带有更多参数的函数,zipWith3,因为它是我真正想到的唯一函数.

We can take a function with more arguments, zipWith3 because it is the only one I can really think of.

zipWith3 :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]

如果我们只是读这本书的内容,它将使用一个函数,该函数接收3个值,并分别返回第四个值,然后返回3个这些值的列表,并返回第四个值的列表.试试吧.

If we just read what this does it takes a function that takes 3 values and returns a fourth and then 3 lists of those values respectively and it returns a list of the fourth value. Trying it out.

add3 :: Int -> Int -> Int -> Int
add3 a b c = a + b + c

Prelude>zipWith3 add3 [1] [2] [3]
[6]

尽管,在这种情况下,所有值都是Int类型,它仍然说明了这一点.

Although, in this case all the values are of type Int it still demonstrates the point.

现在,如果我们不提供所有列表怎么办?如果我们仅给出add3就没有列表怎么办.

Now what if we don't give it all the lists? What if we give it no lists just add3.

zipWith3 add3 :: [Int] -> [Int] -> [Int] -> [Int]
zipWith3 add3 :: [Int] -> ([Int] -> [Int] -> [Int])
zipWith3 add3 :: [Int] -> [Int] -> ([Int] -> [Int])

因此,现在我们有了一个包含3个列表并返回一个列表的函数.但这也是一个接受列表的函数,返回一个接受2个列表并返回一个列表的函数.真的无法区分它们.

So, now we have a function that takes 3 lists and returns a list. But this is also a function that takes a list are returns a function that takes 2 lists and returns a list. There is no way to distinguish between them really.

(zipWith3 add3) [1,2] [3,4] [5,6] :: [Int]
(zipWith3 add3) [1,2] :: [Int] -> [Int] -> [Int]

看看我要去哪里?返回值类型之间没有区别.

See where I'm going with this? There is no distinction between arguments are return types.

这篇关于为什么Haskell类型签名声明有多个箭头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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