;; This file contains excerpts from the textbook Concrete ;; Abstractions: An Introduction to Computer Science Using Scheme, by ;; Max Hailperin, Barbara Kaiser, and Karl Knight, Copyright (c) 1998 ;; by Brooks/Cole Publishing Company. This file may not be reproduced ;; or redistributed other than for use with that textbook. ;; Chapter 7: Lists ;; 7.3 Basic List Processing Techniques (define filter (lambda (ok? lst) (cond ((null? lst) '()) ((ok? (car lst)) (cons (car lst) (filter ok? (cdr lst)))) (else (filter ok? (cdr lst)))))) ;; Chapter 14: Object-Oriented Programming ;; 14.5 An Application: Adventures in the Imaginary Land of Gack (define delq (lambda (item list) (filter (lambda (x) (not (eq? x item))) list))) (define display-message (lambda (list-of-stuff) (newline) (for-each (lambda (s) (display s) (display " ")) list-of-stuff))) (define verbalize-list (lambda (items none-word) (define loop (lambda (items) (if (null? (cdr items)) items (cons (car items) (cons "and" (loop (cdr items))))))) (if (null? items) (list none-word) (loop items)))) (define random-element (lambda (list) (if (null? list) #f (list-ref list (random (length list)))))) ;; THIS IS A PROCEDURE ADDED BY DAVID WOLFE IN FALL 2001 WHICH ;; ATTEMPTS TO BE A BIT MORE INFORMATIVE THAN oops-display. (define oops-describe (lambda (x) (define MAX_LIST_LENGTH 15) (define display-each (lambda (lst) (for-each display lst))) (define display-list (lambda (lst) (define do-it (lambda (lst max-list-length) (cond ((null? lst)) ((= max-list-length 0) (display "...")) ((= (length lst) 1) (oops-display (car lst))) (else (oops-display (car lst)) (display " ") (do-it (cdr lst) (- max-list-length 1)))))) (do-it lst MAX_LIST_LENGTH))) (define describe-verbose (lambda (x) (cond ;((object? x) (object/describe x)) ((vector? x) (display "#") (describe-verbose (vector->list x))) ((or (string? x) (number? x) (symbol? x) (boolean? x)) (write x)) ((list? x) (display "(") (display-list x) (display ")")) ((procedure? x) (display "")) ((pair? x) (display "(") (oops-display (car x)) (display " . ") (oops-display (cdr x)) (display ")")) (else (display-each (list "")))))) (describe-verbose x)))