What is the meaning of (`addTwo` 4) 5? - haskell

With a simple Haskell adder function
addTwo:: Num a => a -> a -> a
addTwo a b = a + b
and following expressions
addTwo 4 5 -- yields 9
4 `addTwo` 5 -- yields 9
(`addTwo` 4) 5 -- yields 9
I understand the first two expressions. However, how does the third one work? Does the expression (`addTwo` 4) become a function of one argument? What is the general principle here?

Does the expression (`addTwo` 4) become a function of one argument?
Yes, this is exactly what happens. This is exactly the same as any other operator section like (+4), just using backticks instead of some other infix operator. In general, (a `op`) is the same as \x -> a `op` x, while (`op` a) is the same as \x -> x `op` a.

Related

what does ' ' mean in this following haskell code? [duplicate]

Just ran into something that seems strange to me. Backticks function as something like a syntactic operator.
applyOp :: Int -> (Int -> Int -> Int) -> Int -> Int
applyOp x op y = x `op` y
> applyOp 2 (+) 5
7
I was surprised to see this. I had always imagined that backticks were required to surround an operator symbol or identifier, not an identifier which can be bound during execution to an operator. Am I thinking about this the wrong way?
Backticks are syntax sugar that turns an identifier into an infix operator. That is to say, a `f` b = f a b is the general rewrite rule. This is useful for clarity, but also allows one to avoid too many brackets, since f (a b) (c d) can be rewritten as a b `f` c d.
However there are a few caveats with using these, but only two restrictions:
The backticked expression must be an identifier, so 1 `mod` 2 is valid, but a `zipWith (+)` b is not valid, because it involved a function application.
Only alphanumeric identifiers can be backticked, so 1 `mod` 2 is valid, but 1 `(+)` 2 is invalid. You could see this as an application of the previous restriction.
Backticked expressions have precedence 9, and are left-associative, so a `f` b `f` c is parsed as (a `f` b) `f` c, and in general other operators will inlcude it, so a +
c `f` b is parsed as a + (c `f` b)*
In this case, applyOp x op y = x `op` y is valid, since op is an alphanumeric identifier, and this is equivalent to applyOp x op y = op x y. Note that there is no restriction on binding pattern-matched identifiers!
*This is untrue for the standard Prelude operators !! and .. More info of precedence and fixity can be found in The Haskell 98 Report.

Is a section the result of currying?

In Programming in Haskell by Hutton
In general, if # is an operator, then expressions of the form (#), (x #), and (# y) for arguments x and
y are called sections, whose meaning as functions can be
formalised using lambda expressions as follows:
(#) = \x -> (\y -> x # y)
(x #) = \y -> x # y
(# y) = \x -> x # y
What are the difference and relation between "section" and "currying"?
Is a section the result of applying the currying operation to a multi-argument function?
Thanks.
A section is just special syntax for applying an infix operator to a single argument. (# y) is the more useful of the two, as (x #) is equivalent to (#) x (which is just applying the infix operator as a function to a single argument in the usual fashion).
curry f x y = f (x,y). uncurry g (x,y) = g x y.
(+ 3) 4 = (+) 4 3 = 4 + 3. (4 +) 3 = (+) 4 3 = 4 + 3.
A section is a result of partial application of a curried function: (+ 3) = flip (+) 3, (4 +) = (+) 4.
A curried function (like g or (+)) expects its arguments one at a time. An uncurried function (like f) expects its arguments in a tuple.
To partially apply an uncurried function we have first to turn it into a curried function, with curry. To partially apply a curried function we don't need to do anything, just apply it to an argument.
curry :: ((a, b) -> c ) -> ( a -> (b -> c))
uncurry :: (a -> (b -> c)) -> ((a, b) -> c )
x :: a
g :: a -> (b -> c)
--------------------
g x :: b -> c
x :: a
f :: (a, b) -> c
---------------------------
curry f :: a -> (b -> c)
curry f x :: b -> c
Left sections and right sections are syntactical devices for partially applying an infix operator to a single argument (see also chepner's answer). For the sake of accuracy, we should note that currying is not the same thing as partial application:
Currying is converting a function that takes N arguments into a function that takes a single argument and returns a function that takes N-1 arguments.
Partial application is making a function that takes N-1 arguments out of a function that takes N arguments by supplying one of the arguments.
In Haskell, it happens that everything is curried; all functions take just one argument (even uncurried functions in Haskell take a tuple, which is, strictly speaking, a single argument -- you might want to play with the curry and uncurry functions to see how that works). Still, we very often think informally of functions that return functions as functions of multiple arguments. From that vantage point, a nice consequence of currying by default is that partial application of a function to its first argument becomes trivial: while, for instance, elem takes a value and a container and tests if the value is an element of the contaier, elem "apple" takes a container (of strings) and tests if "apple" is an element of it.
As for operators, when we write, for instance...
5 / 2
... we are applying the operator / to the arguments 5 and 2. The operator can also be used in prefix form, rather than infix:
(/) 5 2
In prefix form, the operator can be partially applied in the usual way:
(/) 5
That, however, arguably looks a little awkward -- after all, 5 here is the numerator, and not the denominator. I'd say left section syntax is easier on the eye in this case:
(5 /)
Furthermore, partial application to the second argument is not quite as straightforward to write, requiring a lambda, or flip. In the case of operators, a right section can help with that:
(/ 2)
Note that sections also work with functions made into operators through backtick syntax, so this...
(`elem` ["apple", "grape", "orange"])
... takes a string and tests whether it can be found in ["apple", "grape", "orange"].

Backticks as a syntactic operator

Just ran into something that seems strange to me. Backticks function as something like a syntactic operator.
applyOp :: Int -> (Int -> Int -> Int) -> Int -> Int
applyOp x op y = x `op` y
> applyOp 2 (+) 5
7
I was surprised to see this. I had always imagined that backticks were required to surround an operator symbol or identifier, not an identifier which can be bound during execution to an operator. Am I thinking about this the wrong way?
Backticks are syntax sugar that turns an identifier into an infix operator. That is to say, a `f` b = f a b is the general rewrite rule. This is useful for clarity, but also allows one to avoid too many brackets, since f (a b) (c d) can be rewritten as a b `f` c d.
However there are a few caveats with using these, but only two restrictions:
The backticked expression must be an identifier, so 1 `mod` 2 is valid, but a `zipWith (+)` b is not valid, because it involved a function application.
Only alphanumeric identifiers can be backticked, so 1 `mod` 2 is valid, but 1 `(+)` 2 is invalid. You could see this as an application of the previous restriction.
Backticked expressions have precedence 9, and are left-associative, so a `f` b `f` c is parsed as (a `f` b) `f` c, and in general other operators will inlcude it, so a +
c `f` b is parsed as a + (c `f` b)*
In this case, applyOp x op y = x `op` y is valid, since op is an alphanumeric identifier, and this is equivalent to applyOp x op y = op x y. Note that there is no restriction on binding pattern-matched identifiers!
*This is untrue for the standard Prelude operators !! and .. More info of precedence and fixity can be found in The Haskell 98 Report.

is mult_add a real function? What does it do?

I have the following question given to me.
Write a function form_number_back that takes a list of positive integers and forms a decimal number using the numbers in the list in reverse order.
For example form_number_back [1, 2, 3, 4] should return the number 4321;
form_number_back [ ] returns 0
Use the function foldr and mult_add below to accomplish this mult_add d s = d + 10*s
Note: foldr and foldr1 are two different functions. Try to use foldr1 instead of foldr in your definition and see if you get the same results with an empty list. Explain your results.
I cannot find anything on mult_add. I thought mabye it was the function name but she wants form_number_back as the function name. Which means mult_add is a Haskell function.
Can anyone explain to me what mult_add does? Is it even written right? Is mult_add another usermade function i'm supposed to use with my own code?
Edit 2
I tried putting in the function example to get its type..
so..
form_number_back [1, 2, 3, 4] :: Num b => b -> [b] -> b
so my function looks like
form_number_back a = foldr(mult_add)
but is returning type of
form_number_back :: Num b => [t] -> b -> [b] -> b
Trying to figure out how to get rid of that [t]
Types are both more important and more informative in Haskell than in most other languages. When you don't understand Haskell, a good first step is to think about types. So let's do that. We'll fire up ghci and enter:
Prelude> let mult_add d s = d + 10 * s
And now ask for its type:
Prelude> :t mult_add
mult_add :: Num a => a -> a -> a
That is, mult_add takes an a and another a, and returns an a, with the proviso that a is an instance of the Num class (so that you can add and multiply them).
You're asked to use foldr to write this function, so let's look at the type of that:
Prelude> :t foldr
foldr :: (a -> b -> b) -> b -> [a] -> b
That looks a bit intimidating, so let's break it down. The first part, (a -> b -> b) tells us that foldr needs a function of two variables, a and b. Well, we have one of those already - it's mult_add. So what happens if we feed in mult_add as the first argument to foldr?
Prelude> :t foldr mult_add
foldr mult_add :: Num b => b -> [b] -> b
Okay! We now have a function that takes a b and a [b] (a list of bs) and returns a b. The function you're trying to write needs to return 0 when it's given the empty list, so let's try feeding it the empty list, with a few different values for the remaining argument:
Prelude> foldr mult_add 10 []
10
Prelude> foldr mult_add 5 []
5
Hey, that's interesting. If we feed it the number x and the empty list, it just returns x (Note: this is always true for foldr. If we give it the initial value x and the empty list [], it will return x, no matter what function we use in place of mult_add.)
So let's try feeding it 0 as the second argument:
Prelude> foldr mult_add 0 []
0
That seems to work. Now how about if we feed it the list [1,2,3,4] instead of the empty list?
Prelude> foldr mult_add 0 [1,2,3,4]
4321
Nice! So it seems to work. Now the question is, why does it work? The trick to understanding foldr is that foldr f x xs inserts the function f between every element of xs, and additionally puts x at the end of the list, and collects everything from the right (that's why it's called a right fold). So, for example:
foldr f 0 [1,2,3] = 1 `f` (2 `f` (3 `f` 0))
where the backticks indicate that we're using the function in its infix form (so its first argument is the one on the left, and the second argument is the one on the right). In your example you have f = mult_add, which multiplies its second argument by 10 and adds it to the first argument:
d `mult_add` s = d + 10 * s
so you have
foldr mult_add 0 [1,2,3] = 1 `mult_add` (2 `mult_add` (3 `mult_add 0))
= 1 `mult_add` (2 `mult_add` 3)
= 1 `mult_add` 32
= 321
which does what you expect. To make sure you understand this, work out what would happen if you defined mult_add the other way around, i.e.
mult_add d s = 10 * d + s

How does currying work?

I'm very new to Haskell and FP in general. I've read many of the writings that describe what currying is, but I haven't found an explanation to how it actually works.
Here is a function: (+) :: a -> (a -> a)
If I do (+) 4 7, the function takes 4 and returns a function that takes 7 and returns 11. But what happens to 4 ? What does that first function do with 4? What does (a -> a) do with 7?
Things get more confusing when I think about a more complicated function:
max' :: Int -> (Int -> Int)
max' m n | m > n = m
| otherwise = n
what does (Int -> Int) compare its parameter to? It only takes one parameter, but it needs two to do m > n.
Understanding higher-order functions
Haskell, as a functional language, supports higher-order functions (HOFs). In mathematics HOFs are called functionals, but you don't need any mathematics to understand them. In usual imperative programming, like in Java, functions can accept values, like integers and strings, do something with them, and return back a value of some other type.
But what if functions themselves were no different from values, and you could accept a function as an argument or return it from another function? f a b c = a + b - c is a boring function, it sums a and b and then substracts c. But the function could be more interesting, if we could generalize it, what if we'd want sometimes to sum a and b, but sometimes multiply? Or divide by c instead of subtracting?
Remember, (+) is just a function of 2 numbers that returns a number, there's nothing special about it, so any function of 2 numbers that returns a number could be in place of it. Writing g a b c = a * b - c, h a b c = a + b / c and so on just doesn't cut it for us, we need a general solution, we are programmers after all! Here how it is done in Haskell:
let f g h a b c = a `g` b `h` c in f (*) (/) 2 3 4 -- returns 1.5
And you can return functions too. Below we create a function that accepts a function and an argument and returns another function, which accepts a parameter and returns a result.
let g f n = (\m -> m `f` n); f = g (+) 2 in f 10 -- returns 12
A (\m -> m `f` n) construct is an anonymous function of 1 argument m that applies f to that m and n. Basically, when we call g (+) 2 we create a function of one argument, that just adds 2 to whatever it receives. So let f = g (+) 2 in f 10 equals 12 and let f = g (*) 5 in f 5 equals 25.
(See also my explanation of HOFs using Scheme as an example.)
Understanding currying
Currying is a technique that transforms a function of several arguments to a function of 1 argument that returns a function of 1 argument that returns a function of 1 argument... until it returns a value. It's easier than it sounds, for example we have a function of 2 arguments, like (+).
Now imagine that you could give only 1 argument to it, and it would return a function? You could use this function later to add this 1st argument, now encased in this new function, to something else. E.g.:
f n = (\m -> n - m)
g = f 10
g 8 -- would return 2
g 4 -- would return 6
Guess what, Haskell curries all functions by default. Technically speaking, there are no functions of multiple arguments in Haskell, only functions of one argument, some of which may return new functions of one argument.
It's evident from the types. Write :t (++) in interpreter, where (++) is a function that concatenates 2 strings together, it will return (++) :: [a] -> [a] -> [a]. The type is not [a],[a] -> [a], but [a] -> [a] -> [a], meaning that (++) accepts one list and returns a function of type [a] -> [a]. This new function can accept yet another list, and it will finally return a new list of type [a].
That's why function application syntax in Haskell has no parentheses and commas, compare Haskell's f a b c with Python's or Java's f(a, b, c). It's not some weird aesthetic decision, in Haskell function application goes from left to right, so f a b c is actually (((f a) b) c), which makes complete sense, once you know that f is curried by default.
In types, however, the association is from right to left, so [a] -> [a] -> [a] is equivalent to [a] -> ([a] -> [a]). They are the same thing in Haskell, Haskell treats them exactly the same. Which makes sense, because when you apply only one argument, you get back a function of type [a] -> [a].
On the other hand, check the type of map: (a -> b) -> [a] -> [b], it receives a function as its first argument, and that's why it has parentheses.
To really hammer down the concept of currying, try to find the types of the following expressions in the interpreter:
(+)
(+) 2
(+) 2 3
map
map (\x -> head x)
map (\x -> head x) ["conscience", "do", "cost"]
map head
map head ["conscience", "do", "cost"]
Partial application and sections
Now that you understand HOFs and currying, Haskell gives you some syntax to make code shorter. When you call a function with 1 or multiple arguments to get back a function that still accepts arguments, it's called partial application.
You understand already that instead of creating anonymous functions you can just partially apply a function, so instead of writing (\x -> replicate 3 x) you can just write (replicate 3). But what if you want to have a divide (/) operator instead of replicate? For infix functions Haskell allows you to partially apply it using either of arguments.
This is called sections: (2/) is equivalent to (\x -> 2 / x) and (/2) is equivalent to (\x -> x / 2). With backticks you can take a section of any binary function: (2`elem`) is equivalent to (\xs -> 2 `elem` xs).
But remember, any function is curried by default in Haskell and therefore always accepts one argument, so sections can be actually used with any function: let (+^) be some weird function that sums 4 arguments, then let (+^) a b c d = a + b + c in (2+^) 3 4 5 returns 14.
Compositions
Other handy tools to write concise and flexible code are composition and application operator. Composition operator (.) chains functions together. Application operator ($) just applies function on the left side to the argument on the right side, so f $ x is equivalent to f x. However ($) has the lowest precedence of all operators, so we can use it to get rid of parentheses: f (g x y) is equivalent to f $ g x y.
It is also helpful when we need to apply multiple functions to the same argument: map ($2) [(2+), (10-), (20/)] would yield [4,8,10]. (f . g . h) (x + y + z), f (g (h (x + y + z))), f $ g $ h $ x + y + z and f . g . h $ x + y + z are equivalent, but (.) and ($) are different things, so read Haskell: difference between . (dot) and $ (dollar sign) and parts from Learn You a Haskell to understand the difference.
You can think of it like that the function stores the argument and returns a new function that just demands the other argument(s). The new function already knows the first argument, as it is stored together with the function. This is handled internally by the compiler. If you want to know how this works exactly, you may be interested in this page although it may be a bit complicated if you are new to Haskell.
If a function call is fully saturated (so all arguments are passed at the same time), most compilers use an ordinary calling scheme, like in C.
Does this help?
max' = \m -> \n -> if (m > n)
then m
else n
Written as lambdas. max' is a value of a lambda that itself returns a lambda given some m, which returns the value.
Hence max' 4 is
max' 4 = \n -> if (4 > n)
then 4
else n
Something that may help is to think about how you could implement curry as a higher order function if Haskell didn't have built in support for it. Here is a Haskell implementation that works for a function on two arguments.
curry :: (a -> b -> c) -> a -> (b -> c)
curry f a = \b -> f a b
Now you can pass curry a function on two arguments and the first argument and it will return a function on one argument (this is an example of a closure.)
In ghci:
Prelude> let curry f a = \b -> f a b
Prelude> let g = curry (+) 5
Prelude> g 10
15
Prelude> g 15
20
Prelude>
Fortunately we don't have to do this in Haskell (you do in Lisp if you want currying) because support is built into the language.
If you come from C-like languages, their syntax might help you to understand it. For example in PHP the add function could be implemented as such:
function add($a) {
return function($b) use($a) {
return $a + $b;
};
}
Haskell is based on Lambda calculus. Internally what happens is that everything gets converted into a function. So your compiler evaluates (+) as follows
(+) :: Num a => a -> a -> a
(+) x y = \x -> (\y -> x + y)
That is, (+) :: a -> a -> a is essentially the same as (+) :: a -> (a -> a). Hope this helps.

Resources