Docsity

Haskell Function Concepts: Currying, Uncurrying, Function Composition, and Identity - Prof, Study notes of Computer Science

Toggle sidebar
bg1
COSC 3015: Lecture 4
Lecture given by Prof. Caldwell and scribed by Sunil Kothari
September 16, 2008
1 Recap
Somebody asked about what’s going on ? So here is the response. Recall
functions from dom ato codomain b:a→bwhere, a and b are type variables
and they can stand for any type. Suppose we instantiate b with the type
Int →S tring Then if f:: a→(Int →String) so fmaps things of type a
to functions of type Int →S tring. That means if x:: athen
fx :: Int →String
The result of applying the function fto the argument x. Here’s a concrete
example:
Example 1 f k =\m−> m +kHere’s another equivalent definition
f k m =m+kSo the type of fis Num a ⇒a→(a→a)What does flook
like as a set of pairs ?
f0 = \m→m+ 0
f1 = \m→m+ 1
f(−1) = \m→m+ (−1)
In the last HW notes f was called plus.
In Haskell, the default is curried.
>:t plus (x,y) = x + y
Num a => (a,a) -> a
>:t plusc x y = x + y
Num a => a -> (a -> a)
input output
<0 ,<< 0,0>, < 1,1>< −1,−1>,· · · >>
<1 ,<< 0,1>, < 1,2>< −1,0>,· · · >>
.
.
.
1
pf3

Partial preview of the text

Download Haskell Function Concepts: Currying, Uncurrying, Function Composition, and Identity - Prof and more Study notes Computer Science in PDF only on Docsity!

COSC 3015: Lecture 4

Lecture given by Prof. Caldwell and scribed by Sunil Kothari

September 16, 2008

1 Recap

Somebody asked about what’s going on? So here is the response. Recall functions from dom a to codomain b: a → b where, a and b are type variables and they can stand for any type. Suppose we instantiate b with the type Int → String Then if f :: a → (Int → String) so f maps things of type a to functions of type Int → String. That means if x :: a then f x :: Int → String The result of applying the function f to the argument x. Here’s a concrete example:

Example 1 f k = \ m− > m + k Here’s another equivalent definition f k m = m + k So the type of f is N um a ⇒ a → (a → a) What does f look like as a set of pairs?

f 0 = \m → m + 0 f 1 = \m → m + 1 f (−1) = \m → m + (−1)

In the last HW notes f was called plus. In Haskell, the default is curried.

:t plus (x,y) = x + y Num a => (a,a) -> a :t plusc x y = x + y Num a => a -> (a -> a)

input output < 0 ,<< 0 , 0 >, < 1 , 1 >< − 1 , − 1 >, · · · >> < 1 ,<< 0 , 1 >, < 1 , 2 >< − 1 , 0 >, · · · >> .. .

:t plusc 5 Int -> Int\ :t plus 5 ! splat - type error

2 Curry and Uncurry

So curry can be defined as : curryf = \x → \y → f (x, y) Here’s an alternative definition curryf x y = f (x, y) so the type of f is ((a, b) → c) → a → b → c. Remember arrow associates to the right. so, a → b → c means a → (b → c). Also, function application associates to the left. So - f x y means (f x) y and not f (x y) By default, type checker is gonna get rid of as many paranthesis as it can. Normally, pairs are written as catesian product but in Haskell both type and terms are represented by the same notation i.e. depending on the context (a,b) might be a product type or a pair of terms. And uncurry could be written as: uncurry f = \p → f (f st p) (snd p) ¿:t fst Here’s an anternative definition uncurry f = (x, y) → f x y Here’s another definition uncurry f (x, y) = f x y So the type of uncurry is (a → b → c) → (a, b) → c. Again curry and uncurry are fundamental notions. By default, things are written in curried form.

3 Function composition

Consider three types A, B, C with f : A → B, g : B → C so function composition is defined as (g ◦ f )(x) = g(f x) The type of g ◦ f is a → c. To make it syntactic valid Haskell we can write backquotes around it. For example ’◦’. In Haskell, we can write is as g.f. So g.f = \x− > g(f x). so - ”.” is a special infix operator denoting function composition. There is a Haskell notation for turning infix operator into a prefix operator. If ⊕ is an arbitrary binary argument taking a and b to c - then (⊕) :: a → b → c. So, what is the type of function composition (.) :: (b → c) → (a → b) → (a → c)

Example 2 add 1 x = x + 1 times 2 x = 2 ∗ x add 1 .times 2 ; \y− > 2 ∗ y + 1 times 2 .add 1 ; \y− > 2 ∗ (y + 1)

How can we calculate this? what is the type of add 1 .times2? It’s Int → Int. So - choose an arbitrary int (say x) and then calculate with (add 1 .times2)x ; add1(times 2 x) ; add1(2 ∗ x) ; (2 ∗ x) + 1 Now, consider show. One of the types of show is:: Int → String. Consider length :: [a] → Int. So, what is length.show - calculates the length of the string representation of its argument. The type of length.show is (Show a) => a -> Int