Programming Languages (CSC-302 98S)

[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Assignments]

# Outline of Class 22: Other Issues in Functional Languages and Scheme

Held: Friday, March 13, 1998

• Don't forget that assignment 4 is due today. It's okay if assignments are late, but make sure you get your assignment to me before leaving for break.
• You had a question to answer for today: What are some common higher-order functions we encounter (or might encounter)?

## Anonymous Functions

• Recall that a key attribute of high-level languages is abstraction from the underlying machine.
• One aspect of abstraction is that we don't need to name all of the intermediate values we use.
• For example, in evaluating (2+(X*Y-Z/Q/R)), the machine needs to store the results of intermediate computations in termporary locations. However, the high-level language lets us abstract away from those temporary locations and speak only in terms of the more abstract expression.
• Is there something similar we'd like to do for functions? Yes. Often, we need a function only for inclusion within another function (typically, a higher-order function).
• Many functional languages provide lambda abstractions which let us describe functions without naming them.
• "The function with argument x and body b" is typically written lambda x.b (sorry, no greek characters on my keyboard).
• In Scheme, this would be written `(lambda (x) b)`.
• How is this useful? Suppose we wanted to add two to all of the elements of a list. We can now write
```(applyall (lambda (x) (+ x 2)) l)
```

• We can also use lambdas to build new functions from old. Consider function composition (normally written with a smaller circle).
```(define (compose f g) (lambda (x) (f (g x))))
```

• Scheme (and Scheme programmers) uses lambdas freely. Note that we can even write function definitions in terms of lambda. The following two definitions are equivalent.
```(define (fun args) body)
(define fun (lambda (args) body))
```

### Currying

• A mathematician nameed Haskell B. Curry observed that there are many ways to think of functions of multiple arguments, particularly when we're working with "higher-order" functions.
• A function of two arguments can be thought of as a function from pairs of elements to whatever.
• A function of two arguments can be thought of as a function of one argument that returns another function of one argument that returns a value.
• Consider a `add` function.
• We might write
`(define add (lambda (x y) (+ x y)))`
• We might write
`(define add (lambda (x) (lambda (y) (+ x y)))) `
• The second version is called the Curried version of the function.
• No it's not spicier.
• You have to apply curried functions slightly differently. Rather than writing `(add 3 4)`, you would write `((add 3) 4)`. (Some other functional languages provide more elegant notations.)
• Curried functions are quite useful, because you can partially apply them.
• Consider `(define lt (lambda (x) (lambda (y) (< x y))))`. We might use this as part of an implementation of quicksort to extract the smaller and bigger elements in a list.
```;;; Determine if the first "argument" is smaller than the second
(define lt (lambda (x) (lambda (y) (< x y))))
(define gt (lambda (x) (lambda (y) (> x y))))
;;; Select all the elements of a list that match a predicate
(define (select pred l)
(if (null? l) nil
(if (pred (car l))
(cons (car l) (select pred (cdr l)))
(select pred (cdr l)))))
;;; Select the elements of a list larger than a particular value
(define larger (lambda (n) (lambda (l) (select (lt n) l))))
;;; Select the elements of a list smaller than a particular value
(define smaller (lambda (n) (lambda (l) (select (gt n) l))))
;;; Select the equal elements of a list
(define same (lambda (n) (lambda (l)
(select (lambda (x) (= n x)) l))))
;;; Quick sort a list
(define (qsort l)
(if (null? l) nil
(append (qsort ((smaller (car l)) l))
((same (car l)) l)
(qsort ((larger (car l)) l)))))
```

On to Continuations
Back to Functional Programming: The Scheme Perspective
Outlines: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
Current position in syllabus

[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Assignments]

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.