Docsity

Understanding Function Currying and Uncurrying in Haskell - Prof. James L. Caldwell, Assignments of Computer Science

Toggle sidebar
bg1
HW 3 Prof. Caldwell
Due: 4 September 2008 COSC 3015
1
Problem 1.1. When you get your books, read chapters 1 and 2.
If you have not ordered a copy of the book yet – you should.
2
In class we discussed the following code.
plus :: (Integer, Integer) -> Integer
plus (x,y) = x + y
plusc :: Integer -> Integer -> Integer
pluscxy=x+y
The function plus takes its arguments all at once packaged in a pair while plusc takes its
arguments one at a time.
We discussed how Haskell supports a notation for describing a function without forcing you to
choose a name for it.
The general form is
\x→e
where xis a variable and eis a Haskell expression.
Note that in Haskell “→” is used to denote the type constructor for functions (e.g. if γand δ
are types, then γ→δis the type of functions from γto δ. Also, “→” is used in the expression
language to describe an actual function, (\x→e) denotes a function whose single argument is
referred to in the expression eby the variable x.
This overloading of syntax is similar to that for Cartesian products. If γand δare types then
(γ, δ ) is the type whose elements are the pairs where the first element comes from γand the second
element comes from δ. But also, if a∈γand b∈δ, then the pair (a, b)∈(γ , δ). So the developers
of Haskell have used the same notation for the type constructor and to construct the elements of
the type in both cases.
Now, consider the following interaction with the Haskell interpreter.
Main> :type plusc
plusc :: Integer -> Integer -> Integer
Main> :type plusc 7
plusc 7 :: Integer -> Integer
Evidently plusc 7 is a function of type Integer -> Integer. But what function is it? It is the
function that is expecting an input yand will compute the expression 7+y. So, it is the function
described by the following expression:
\y->7+y
1
pf3

Partial preview of the text

Download Understanding Function Currying and Uncurrying in Haskell - Prof. James L. Caldwell and more Assignments Computer Science in PDF only on Docsity!

HW 3 Prof. Caldwell Due: 4 September 2008 COSC 3015

Problem 1.1. When you get your books, read chapters 1 and 2. If you have not ordered a copy of the book yet – you should.

In class we discussed the following code.

plus :: (Integer, Integer) -> Integer plus (x,y) = x + y

plusc :: Integer -> Integer -> Integer plusc x y = x + y

The function plus takes its arguments all at once packaged in a pair while plusc takes its arguments one at a time. We discussed how Haskell supports a notation for describing a function without forcing you to choose a name for it. The general form is \x → e

where x is a variable and e is a Haskell expression. Note that in Haskell “→” is used to denote the type constructor for functions (e.g. if γ and δ are types, then γ → δ is the type of functions from γ to δ. Also, “→” is used in the expression language to describe an actual function, (\x → e) denotes a function whose single argument is referred to in the expression e by the variable x. This overloading of syntax is similar to that for Cartesian products. If γ and δ are types then (γ, δ) is the type whose elements are the pairs where the first element comes from γ and the second element comes from δ. But also, if a ∈ γ and b ∈ δ, then the pair (a, b) ∈ (γ, δ). So the developers of Haskell have used the same notation for the type constructor and to construct the elements of the type in both cases. Now, consider the following interaction with the Haskell interpreter.

Main> :type plusc plusc :: Integer -> Integer -> Integer Main> :type plusc 7 plusc 7 :: Integer -> Integer

Evidently plusc 7 is a function of type Integer -> Integer. But what function is it? It is the function that is expecting an input y and will compute the expression 7 + y. So, it is the function described by the following expression:

\y -> 7 + y

This form of Haskell expression is called a lambda-term (λ-term). We can write (\x y → e) for (\x → (\y → e)). Every function can be written in a form where no arguments are declared on the left side of the definition. If e is an arbitrary Haskell expression, then the following examples show how this works. f x = e is the same as f = \x → e g x y = e is the same as g x = \y → e is the same as g = \x y → e We say that plusc is in Curry Form. In this form of function definition, where the function takes its arguments one at a time, is named after Haskell Curry (1900-1982), an American mathematician and logician. As you might guess, the Haskell programming language is named after him as well. It takes its arguments one at a time – as opposed to packaged up in a pair. Consider the following function (as defined in class):

curry f x y = f(x,y)

In Haskell we have the following.

Main> :t curry curry :: ((a,b) -> c) -> a -> b -> c Main>

So curry takes a function of type ((a,b) -> c) and returns a function of type (a -> b -> c). Also, consider the following definition:

uncurry f p = f (fst p) (snd p)

Or equivalently,

uncurry f (x,y) = f x y

Here is the type of uncurry.

Main> :t uncurry uncurry :: (a -> b -> c) -> (a,b) -> c Main>

Now, these functions work on functions of 2 arguments. What about functions of three argu- ments?

Problem 2.1. Write the function curry3 having the following type:

Main> :t curry :t curry curry3 :: ((a,b,c) -> d) -> a -> b -> c -> d Main>