Lecture overview -- Keyboard shortcut: 'u'  Previous page: Higher-order functions -- Keyboard shortcut: 'p'  Next page: Linear search in lists -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Help page about these notes  Alphabetic index  Course home    Lecture 4 - Page 3 : 34
Functional Programming in Scheme
Higher-order Functions
Some simple and general higher-order functions

It is time to look at some examples of higher-order functions. We start with a number of simple ones.

(define (flip f)
  (lambda (x y)
    (f y x)))

The function flip changes the order of it's parameters. The function takes a function of two parameters, and returns another function of two parameters. The only difference between the input and output function of flip is the ordering of their parameters.

y:/Kurt/Files/courses/prog3/prog3-03/sources/notes/includes/flip-alternative.scmAn alternative formulation of flip without use of the sugared define syntax.

For easy comparison we show the original version below the alternative formulation. The two different syntaxes are discussed in an earlier lecture, cf. the cross references.

(define (negate p)
  (lambda (x) 
    (if (p x) #f #t)))

The function negate negates a predicate. Thus, negate takes a predicate function (boolean function) as parameter and returns the negated predicate. The resulting negated predicate returns true whenever the input predicate returns false, and vise versa.

(define (compose f g)
  (lambda (x)
    (f (g x))))

The function compose composes two functions which both are assumed to take a single argument. The resulting function composed of f and g returns f(g(x)), or in Lisp (f (g x)), given the input x . The compose function from the general LAML library accepts two or more parameters, and as such it is more general than the compose function shown here.

Go to exerciseUsing flip, negate, and compose