Functional Programming Lecture 11 - higher order functions - PowerPoint PPT Presentation

About This Presentation
Title:

Functional Programming Lecture 11 - higher order functions

Description:

curry and uncurry are defined in the standard prelude. E.g. Prelude uncurry ( ) (2,1) 3. Finally, we would expect that curry and uncurry are inverses: ... – PowerPoint PPT presentation

Number of Views:103
Avg rating:3.0/5.0
Slides: 14
Provided by: muffyc
Category:

less

Transcript and Presenter's Notes

Title: Functional Programming Lecture 11 - higher order functions


1
Functional ProgrammingLecture 11 - higher
order functions
2
Rule of Cancellation
  • If f t1 -gt t2 -gt -gt tn -gt t
  • and f is applied to arguments
  • e1 t1, ek tk, where kltn
  • then the resulting type is given by cancelling
    out the types t1 to tk
  • t1 -gt t2 -gt -gt tk -gt tk1 -gt tn-gt t
  • i.e.
  • (f e1 ek ) tk1 -gt tk2 -gt tn
  • Example max Int -gt Int -gt Int
  • So, max 2 Int -gt Int

3
  • Example max Int -gt Int -gt Int
  • So, max 2 Int -gt Int
  • (max 2) is just a function.
  • It is the function that takes the maximum of 2
    and another number.
  • given
  • max x y if xgty then x else y
  • then
  • (max 2) y if 2gty then 2 else y
  • This is an example of partial evaluation. (The
    function max has been partially evaluated, to
    give another function, max 2.)

4
Function Application and -gt
  • Associativity
  • Function application is left associative.
  • i.e. f x y (f x) y
  • f x y f (x y) !!
  • E.g.
  • max Int -gt Int -gt Int
  • max 2 3 (max 2) 3
  • max (2 3) (doesnt make sense)

5
Function Application and -gt
  • Associativity
  • Function space symbol -gt is right associative.
  • i.e. a -gt b -gt c means a -gt (b -gt c)
  • not (a -gt b) -gt
    c
  • f a -gt (b -gt c)
  • g (a -gt b) -gt c
  • a
  • b f c (a-gtb)
    g c
  • E.g.
  • max Int -gt (Int -gt Int)
  • max 2 Int -gt Int

6
Partial evaluation
  • When you partially evaluate a function, the new,
    resulting function must be enclosed in ( ..
    ).
  • The new function is also called an operator
    section.
  • Examples
  • (2) the function which adds 2 to its argument.
  • (2) the function which adds 2 to its argument.
  • (gt2) the function which compares its argument
    with 2.
  • (3) the function the puts 3 at the front of a
    list.
  • (\n) the function which puts a newline at the
    end of a string.

7
An aside
  • Displaying strings with newline characters.
  • \n is the newline character.
  • If you type in
  • gt show test\ntest
  • the result is test\ntest
  • To display a string, and interpret the special
    symbols, use putStr String -gt IO String.
  • If you type
  • gt putStr test\ntest
  • the result is
  • test
  • test IO ()

8
Anonymous functions
  • Functions do not have to have names.
  • E.g.
  • \m -gt nm
  • This defines a function with one argument that
    returns the sum of n and that argument.
  • The arguments - given between \ and -gt
  • The result - given after the -gt.
  • \ stands for the Greek letter l.

9
Using a Lambda-defined function
  • Define a function comp2 which applies a function
    f to each of its 2 arguments before applying
    function g to the results.
  • f

  • g
  • f
  • comp2 (a -gt b) -gt (b -gt b -gt c) -gt (a -gt a -gt
    c)
  • f g
    resulting function
  • comp2 f g \x y -gt g (f x) (f y)
  • e.g.
  • comp2 sq add 3 4 gt add (sq 3) (sq 4) gt 916 gt
    25

10
Curry and Uncurry
  • A function which takes its arguments one at a
    time is said to be a curried function, or in
    curried form.
  • (Named after Haskell Curry, a logician who worked
    on combinatory logic and showed relationships to
    l-calculus.)
  • E.g. max Int -gt Int -gt Int
  • is in curried form.
  • x
  • y
  • Most of the functions in this course so far have
    been curried.
  • A function which takes all its arguments
    bundled together as a tuple is said to an
    uncurried function, or in uncurried form.
  • E.g. maxUC (Int,Int) -gt Int
  • is in uncurried form.
  • (x,y)

11
  • There is an obvious relationship between the
    curried and uncurried versions.
  • Namely, max x y maxUC (x,y)
  • We can turn an uncurried function into a curried
    one, and vice-versa.
  • Suppose g is an uncurried function, g (a,b)
    -gtc.
  • Then (curry g) a -gt b -gt c.
  • curry ((a,b) -gtc) -gt (a -gt b -gt c)
  • curry g x y g (x,y)
  • Recall function application is left assoc.
  • i.e. (curry g) x y g (x,y)

12
  • Similarly,
  • suppose f is an curried function, f a -gt b
    -gtc.
  • Then (uncurry f) (a,b) -gt c.
  • uncurry (a -gt b -gt c) -gt ((a,b) -gtc)
  • uncurry f (x,y) f x y
  • So, (uncurry f) (x,y) f x y
  • curry and uncurry are defined in the standard
    prelude.
  • E.g.

Preludegt uncurry () (2,1) 3
13
  • Finally,
  • we would expect that curry and uncurry are
    inverses
  • E.g.
  • curry (uncurry f) f
  • uncurry (curry g) g
  • It works for specific cases
  • uncurry max maxUC
  • curry maxUC max
  • So, curry (uncurry max) curry maxUC max
  • uncurry (curry maxUC) uncurry max maxUC
  • But to prove it for an arbitrary function,
    requires induction.
Write a Comment
User Comments (0)
About PowerShow.com