简体   繁体   中英

Haskell - Struggle understanding types

I am new to Haskell, but it was really fun until now. Currently I am working on understanding Types and Type Classes

Example: add :: Integer -> Integer -> Integer . -> is right associative which means the declaration is similar to Integer -> (Integer -> Integer) , so far so good. But what does (a->b) -> a -> b mean? 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 fx = fx . But I don't understand that, isn't (a->b) a single function?

I know that a and b are Typevariables which indicates that a and b are of different Types.

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 . c just happens to be a type which is a function itself.

The same way your first example Integer -> (Integer -> Integer) was a function that takes an Integer and returns a function 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 .

In the case of this function apply it is simply function application. If apply is defined as apply fx = fx it simply passes argument x to the function f . By the way, this function already exists in the Prelude and is called ($) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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