- A+

I'm trying to learn haskell after years of OOP. I'm reading Happy Haskell. It provides this code:

`plus :: Int -> Int -> Int plus x y = x + y plus' :: Int -> Int -> Int plus' = /x -> /y -> x + y increment :: Int -> Int increment = plus 1 increment' :: Int -> Int increment' = (/x -> /y -> x + y) 1 `

I understand how plus and plus' work (they're the same, different syntax). But increment, I don't get.

`increment :: Int -> Int `

means it takes an int, and returns an int, right? But right after that, the actual function is:

`increment = plus 1 `

Question:

Where is the integer value increment takes? Shouldn't there be an `x`

or something on the right of the `=`

sign, to signify the integer value the function takes as input? Something like:

`increment _ = plus 1 x `

Edit: Also, shouldn't the definition of increment be `Int -> (Int -> Int)`

since it takes an `int`

and passes it to a function that takes an `int`

and returns and `int`

?

# Partial Application

In Haskell, you can have currying and partial application of functions. Have a look a the Haskell Wiki: Partial Application

In particular, if you look at the type signature of any function, there's no real distinction between its inputs (arguments) and its outputs, and this is because really your function `plus :: Int -> Int -> Int`

is a function that, when given an Int, will return another function which itself takes the remaining arguments and returns the int: `Int -> Int`

. This is called **partial application**

This means that when you call `increment = plus 1`

you are saying that increment is equal to -- remember the partial application -- a function (returned by `plus 1`

) which itself take an integer and returns an integer.

As Haskell is a functional programming language, everything with an equal is not an assignment, but more like a definition, so an easy way to understand partial application is really to follow the equal signs:

`increment = plus 1 = plus 1 y = 1 + y `

# Main uses

As you can see, partial application can be used to defined more specific functions, like add 1 to a number which is more specific than just add two numbers. It also allows more use of point-free style, where you concatenate more than one function.

Also note that with infix functions lke `(+)`

, you can partially apply to either the left or the right, which can be useful for non-commutative functions, for example

`divBy2 :: Float -> Float divBy2 = (/2) div2by :: Float -> Float div2by = (2/) Prelude> divBy2 3 1.5 Prelude> div2by 2 1.0 `