For this function :

plus1 = (+) 1

I can use :

Prelude> plus1 3
4

The type of plus1 is

plus1 :: Integer -> Integer

How is function parameter Integer being allowed here ? In my function definition where is the Integer function parameter defined ?

I'm not sure what I expect to happen but coming from Java all parameters need to be defined as part of the method but there seems to be something going on behind the scenes here ?

有帮助吗?

解决方案

(If we ignore the Num typeclass for a moment and pretend that + works on Integers only for simplicity's sake,) (+) is a function of type Integer -> (Integer -> Integer) (the parentheses aren't necessary because -> is right-associative, but I've added them for clarity). That means it takes an argument of type Integer and produces a function of type Integer -> Integer as its result.

So when you apply (+) to the Integer 1, you do indeed get a function of that type as the result. Since add1 holds that result, it is thus a function of type Integer -> Integer and that's why you can apply it as plus1 3.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top