;;; File: ;;; timing-sum.scm ;;; Author: ;;; Samuel A. Rebelsky ;;; Version: ;;; 1.0 of February 2004 ;;; Contents: ;;; sample-values - a vector of sample values ;;; (right-section binproc right) - fill in the second parameter ;;; to a two-parameter procedure, thereby creating a one-parameter ;;; procedure. ;;; (sample-list length) - build a sample list of the appropriate length ;;; (random-list length) - build a random list of numbers between 0 and ;;; 100 of the specified length ;;; (list-and lst) - compute the logical "and" of lst. ;;; Value: ;;; sample-values ;;; Type: ;;; vector of Scheme values (define sample-values (vector 0 1 2 3 4 5 6 7 8 9 'a 'b 'c)) ;;; Procedure: ;;; right-section ;;; DOCUMENTATION ELSEWHERE (define right-section (lambda (binproc right) (lambda (left) (binproc left right)))) ;;; Procedure: ;;; make-list ;;; Parameters: ;;; length, a non-negative integer ;;; proc, a zeroary procedure ;;; Purpose: ;;; To compute a list of the specified length by repeatedly applying ;;; proc (which, presumably, returns a different value each time). ;;; Produces: ;;; lst, a list ;;; Preconditions: ;;; [Standard] ;;; Postconditions: ;;; lst has length length. ;;; Each element of lst was created by a *separate* call to proc. (define make-list (lambda (length proc) (if (zero? length) null (cons (proc) (make-list (- length 1) proc))))) ;;; Procedure: ;;; sample-list ;;; Parameters: ;;; length, a non-negative integer ;;; Purpose: ;;; Build a sample list of the specified length. ;;; Produces: ;;; lst, a list ;;; Preconditions: ;;; sample-values is defined ;;; Postconditions: ;;; lst has length elements. ;;; Each element of lst is selected from sample-values. ;;; The elements of lst are hard to predict. (define sample-list (right-section make-list (lambda () (vector-ref sample-values (random (vector-length sample-values)))))) ;;; Procedure: ;;; random-list ;;; Parameters: ;;; length, a non-negative integer ;;; Purpose: ;;; Build a "random" list of integers in the range [0..100] of the ;;; specified length. ;;; Produces: ;;; lst, a list ;;; Preconditions: ;;; [Standard] ;;; Postconditions: ;;; lst has length elements. ;;; Each element of lst is between 0 and 100. ;;; The elements of lst are hard to predict. (define random-list (right-section make-list (lambda () (random 101)))) ;;; Procedure: ;;; list-and ;;; Parameters: ;;; lst, a list of Scheme values ;;; Purpose: ;;; To compute the semi-logical "and" of a list of values. ;;; Produces: ;;; all-true, a Scheme value ;;; Preconditions: ;;; [Standard] ;;; Postconditions: ;;; if lst is empty, all-true is #t ;;; if lst contains #f, all-true is #f ;;; if lst contains only non-#f values, all-true is the last such value (define list-and (lambda (lst) (if (null? lst) #t (let kernel ((lst lst)) (cond ((null? (cdr lst)) (car lst)) ((not (car lst)) #f) (else (kernel (cdr lst)))))))) ;;; Procedure: ;;; repeat ;;; Parameters: ;;; exp, a Scheme expression to be evaluated. ;;; repetitions, a non-negative integer. ;;; Purpose: ;;; Evaluate exp repetitions times. ;;; Produces: ;;; results, a list ;;; Preconditions: ;;; exp is a valid Scheme expression. ;;; Postconditions: ;;; results has repetitions elements. ;;; Each element of results was created by evaluating exp. ;;; Practica: ;;; > (define numbers (random-list 200000)) ;;; > (repeat '(time (suml numbers)) 10) (define repeat (lambda (exp repetitions) (if (zero? repetitions) null (cons (eval exp) (repeat exp (- repetitions 1)))))) ;;; Procedures: ;;; suml ;;; sumr ;;; Parameters ;;; numbers, a list of numbers of the form (n1 n2 ... nk) ;;; Purpose: ;;; Sum the values in numbers. ;;; Produces: ;;; result, a number ;;; Preconditions: ;;; numbers is nonempty. ;;; Postconditions: ;;; result = n1 + n2 + ... + nk ;;; If any of n1 ... nk is inexact, result is inexact. ;;; If all of n1 ... nk are exact, result is exact. (define sumr (lambda (lst) (if (null? (cdr lst)) (car lst) (+ (car lst) (sumr (cdr lst)))))) (define suml (lambda (lst) (let kernel ((remaining (cdr lst)) (result (car lst))) (if (null? remaining) result (kernel (cdr remaining) (+ result (car remaining))))))) ;;; Procedure: ;;; all-real? ;;; Parameters: ;;; lst, a list of Scheme values ;;; Purpose: ;;; Determine if lst contains only real values ;;; Produces: ;;; result, a truth value ;;; Preconditions: ;;; [None] ;;; Postconditions: ;;; if lst contains a non-real value, result is #f. ;;; otherwise, result is #t. (define all-real? (lambda (lst) (or (null? lst) (and (real? (car lst)) (all-real? (cdr lst)))))) ;;; Procedures: ;;; verified-sum-1 ;;; verified-sum-2 ;;; verified-sum-3 ;;; Parameters ;;; numbers, a list of numbers of the form (n1 n2 ... nk) ;;; Purpose: ;;; Sum the values in numbers. ;;; Produces: ;;; result, a number ;;; Preconditions: ;;; numbers is nonempty. ;;; Postconditions: ;;; result = n1 + n2 + ... + nk ;;; If any of n1 ... nk is inexact, result is inexact. ;;; If all of n1 ... nk are exact, result is exact. (define verified-sum-1 (lambda (lst) (cond ((null? lst) 0) ((not (all-real? lst)) (error 'verified-sum-1 "Parameter must be a list of reals")) (else (+ (car lst) (verified-sum-1 (cdr lst))))))) (define verified-sum-2 (lambda (lst) (cond ((null? lst) 0) ((not (all-real? lst)) (error 'verified-sum-2 "Parameter must be a list of reals")) (else (let kernel ((lst lst)) (if (null? (cdr lst)) (car lst) (+ (car lst) (kernel (cdr lst))))))))) (define verified-sum-3 (lambda (lst) (cond ((null? lst) 0) ((not (real? (car lst))) (error 'verified-sum-3 "Parameter must be a list of reals")) (else (+ (car lst) (verified-sum-3 (cdr lst))))))) (define foo (random-list 2000))