Held: Friday, March 13, 1998
(lambda (x) b).
(applyall (lambda (x) (+ x 2)) l)
(define (compose f g) (lambda (x) (f (g x))))
(define (fun args) body) (define fun (lambda (args) body))
add function.
(define add (lambda (x y) (+ x y)))
(define add (lambda (x) (lambda (y) (+ x y))))
(add 3 4), you would write
((add 3) 4). (Some other functional languages provide
more elegant notations.)
(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)))))
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.
Source text last modified Thu May 7 20:29:41 1998.
This page generated on Thu May 7 20:34:43 1998 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu