简体   繁体   English

Haskell - 斗争理解类型

[英]Haskell - Struggle understanding types

I am new to Haskell, but it was really fun until now. 我是Haskell的新手,但直到现在它真的很有趣。 Currently I am working on understanding Types and Type Classes 目前我正致力于理解类型和类型类

Example: add :: Integer -> Integer -> Integer . 示例: add :: Integer -> Integer -> Integer -> is right associative which means the declaration is similar to Integer -> (Integer -> Integer) , so far so good. ->是右关联,这意味着声明类似于Integer -> (Integer -> Integer) ,到目前为止一直很好。 But what does (a->b) -> a -> b mean? 但是(a->b) -> a -> b是什么意思? Why do we use parenthesis suddenly? 为什么我们突然使用括号? In my textbook there is an example for this declaration with a function apply::(a->b)-> a->b with the def. 在我的教科书中,这个声明的例子有一个函数apply::(a->b)-> a->b with def。 apply fx = fx . apply fx = fx But I don't understand that, isn't (a->b) a single function? 但我不明白,是不是(a->b)单一功能?

I know that a and b are Typevariables which indicates that a and b are of different Types. 我知道a和b是Typevariables,表明a和b是不同的类型。

Whenever you see parenthesis in the type signature you can think of it as one block. 每当您在类型签名中看到括号时,您都可以将其视为一个块。 So (a -> b) -> a -> b is the same as c -> a -> b where c stands for a -> b . 所以(a -> b) -> a -> bc -> a -> b相同,其中c代表a -> b c just happens to be a type which is a function itself. c恰好是一个函数本身的类型。

The same way your first example Integer -> (Integer -> Integer) was a function that takes an Integer and returns a function Integer -> Integer ; 你的第一个例子Integer -> (Integer -> Integer)就是一个函数,它接受一个Integer并返回一个函数Integer -> Integer ; your function (a -> b) -> a -> b is a function that takes as argument a function a -> b and an argument a in order to return a b . 你的函数(a -> b) -> a -> b是一个函数,它将函数a -> b和参数a作为参数,以便返回b

In the case of this function apply it is simply function application. 在这个功能的情况下apply它只是功能应用程序。 If apply is defined as apply fx = fx it simply passes argument x to the function f . 如果apply被定义为apply fx = fx它只是将参数x传递给函数f By the way, this function already exists in the Prelude and is called ($) . 顺便说一下,这个函数已经存在于Prelude中并被称为($)

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

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