[Current] [News] [Glance] [Discussions] [Instructions] [Search] [Links] [Handouts] [Outlines] [Readings] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [Fall2000.01] [Spring2000]
Back to Local procedure bindings, continued. On to Procedures as values, revisited.
Held Wednesday, October 25, 2000
Summary
Today we will discuss procedures that take other procedures as parameters.
Notes
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2000F/Examples/courses.ss
Overview
;;; Add 5 to every value in a list of numbers. ;;; Parameters: ;;; A list of numbers. ;;; Returns: ;;; A list of numbers. ;;; Preconditions: ;;; The list contains only numbers. [Unchecked] ;;; Postconditions: ;;; The result list is the same length as the parameter. ;;; Each element of the result list is five greater than the ;;; corresponding element of the parameter. (define add-five-to-all (lambda (lst) (if (null? lst) null (cons (+ 5 (car lst)) (add-five-to-all (cdr lst)))))) ;;; Scale every value in a list of numbers by 5/4. ;;; Parameters: ;;; A list of numbers. ;;; Returns: ;;; A list of numbers. ;;; Preconditions: ;;; The list contains only numbers. [Unchecked] ;;; Postconditions: ;;; The result list is the same length as the parameter. ;;; Each element of the result list is 5/4 the value of the ;;; corresponding element of the parameter. (define scale-all-by-five-forths (lambda (lst) (if (null? lst) null (cons (* (/ 5 4) (car lst)) (scale-all-by-five-forths (cdr lst))))))
(define do-something-to-all (lambda (lst) (if (null? lst) null (cons (PROCEDURE (car lst)) (do-something-to-all (cdr lst))))))
> (define PROCEDURE (lambda (val) (+ val 2))) > (PROCEDURE 4) 6 > (do-something-to-all '(1 2 3)) (3 4 5) > (define PROCEDURE (lambda (val) (* (/ 5 4) val))) > (PROCEDURE 4) 5 > (do-something-to-all '(1 2 3)) (5/4 5/2 15/4)
;;; Apply a procedure to every value in a list of numbers. ;;; Parameters: ;;; A list of numbers. ;;; A procedure that takes a number as a parameter and returns a number. ;;; Returns: ;;; A list of numbers. ;;; Preconditions: ;;; The list contains only numbers. [Unchecked] ;;; Postconditions: ;;; The result list is the same length as the parameter. ;;; Each element of the result list is the result of applying the ;;; procedure to the corresponding element of the parameter. (define apply-all (lambda (proc lst) (if (null? lst) null (cons (proc (car lst)) (apply-all proc (cdr lst))))))
> (define square (lambda (x) (* x x))) > (apply-all square '(1 2 3 4)) (1 4 9 16)
map
).
square
is just a helper
procedure.
> (let ((square (lambda (x) (* x x)))) (apply-all square '(1 2 3 4)))
square
is
another name for (lambda (x) (* x x))
and then uses
that name.
(apply-all (lambda (x) (* x x)) '(1 2 3 4))
Thursday, 24 August 2000
Wednesday, 25 October 2000
Back to Local procedure bindings, continued. On to Procedures as values, revisited.
[Current] [News] [Glance] [Discussions] [Instructions] [Search] [Links] [Handouts] [Outlines] [Readings] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [Fall2000.01] [Spring2000]
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.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2000F/Outlines/outline.32.html
Source text last modified Wed Oct 25 10:09:43 2000.
This page generated on Wed Oct 25 10:54:24 2000 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu