;;; File: ;;; analysis-examples.scm ;;; Authors: ;;; Samuel A. Rebelsky ;;; Version: ;;; 0.1 of 12 November 2006 ;;; Summary: ;;; Some examples to show the different styles of analysis. For each ;;; sample procedure, there are four versions: ;;; * The procedure, without a numeric suffix (e.g. filter) ;;; -> The base version of the procedure ;;;; * The procedure, with a suffix of 0 (e.g., filter0) ;;; -> The procedure, annotated using (analyst 'count! name) ;;; * The procedure, with a suffix of 1 (e.g., filter1) ;;; -> The procedure, annotated using \$ for most internal procedure calls. ;; * The procedure, with a suffix of 2 (e.g., filter2) ;;; -> The procedure, defined with define\$. ;;; History: ;;; At end. ;;; Contents: ;;; (filter pred? lst) ;;; (filter0 pred? lst) ;;; (filter1 pred? lst) ;;; (filter2 pred? lst) ;;; Filter out all versions of pred? from lst. ;;; Practica: ;;; (analyze (filter2 even? (list 1 2 3 4 5 6 7))) ; +-----------+------------------------------------------------------------ ; | Libraries | ; +-----------+ (load "/home/rebelsky/Web/Courses/CS151/2006F/Examples/analyst.scm") ; +--------+--------------------------------------------------------------- ; | Filter | ; +--------+ ;;; Procedures: ;;; filter ;;; filter0 ;;; filter1 ;;; filter2 ;;; Parameters: ;;; pred?, a unary predicate ;;; lst, a list ;;; Purpose: ;;; Filter out the elements of lst for which pred? holds. ;;; Produces: ;;; filtered, a list. ;;; Preconditions: ;;; pred? can be applied to every element of lst. ;;; Postconditions: ;;; Every elemenet of filtered is an element of lst. ;;; pred? holds on no element of filtered. ;;; If pred? holds on an element of lst, then that element is in filtered. (define filter (lambda (pred? lst) (cond ((null? lst) null) ((pred? (car lst)) (filter pred? (cdr lst))) (else (cons (car lst) (filter pred? (cdr lst))))))) (define filter0 (lambda (pred? lst) (analyst 'count! 'filter) (cond ((null? lst) null) ((pred? (car lst)) (filter0 pred? (cdr lst))) (else (cons (car lst) (filter0 pred? (cdr lst))))))) (define filter1 (lambda (pred? lst) (cond (((\$ null?) lst) null) (((\$ pred?) ((\$ car) lst)) (filter1 pred? ((\$ cdr) lst))) (else ((\$ cons) ((\$ car) lst) (filter1 pred? ((\$ cdr) lst))))))) (define\$ filter2 (lambda (pred? lst) (cond ((null? lst) null) ((pred? (car lst)) (filter2 pred? (cdr lst))) (else (cons (car lst) (filter2 pred? (cdr lst))))))) ; +--------+--------------------------------------------------------------- ; | Length | ; +--------+ (define length (lambda (lst) (if (null? lst) 0 (+ 1 (length (cdr lst)))))) (define length0 (lambda (lst) (analyst 'count! 'length0) (if (null? lst) 0 (+ 1 (length0 (cdr lst)))))) (define length1 (lambda (lst) (if ((\$ null)? lst) 0 ((\$ +) 1 ((\$ length1) ((\$ cdr) lst)))))) (define\$ length2 (lambda (lst) (if (null? lst) 0 (+ 1 (length2 (cdr lst)))))) ; +-------+---------------------------------------------------------------- ; | Tally | ; +-------+ (define tally (lambda (pred? lst) (cond ((null? lst) 0) ((pred? (car lst)) (+ 1 (tally pred? (cdr lst)))) (else (tally pred? (cdr lst)))))) (define tally1 (lambda (pred? lst) (analyst 'count! 'tally1) (cond (((\$ null?) lst) 0) (((\$ pred?) ((\$ car) lst)) ((\$ +) 1 ((\$ tally1) pred? ((\$ cdr) lst)))) (else ((\$ tally1) pred? ((\$ cdr) lst)))))) (define\$ tally2 (lambda (pred? lst) (cond ((null? lst) 0) ((pred? (car lst)) (+ 1 (tally2 pred? (cdr lst)))) (else (tally2 pred? (cdr lst)))))) (define\$ tally-vec (letrec ((kernel (lambda (pred? vec pos len) (cond ((= pos len) 0) ((pred? (vector-ref vec pos)) (+ 1 (kernel pred? vec (+ pos 1) len))) (else (kernel pred? vec (+ pos 1) len)))))) (lambda (pred? vec) (kernel pred? vec 0 (vector-length vec))))) ; +---------+--------------------------------------------------------------- ; | History | ; +---------+ ; Saturday, 11 November 2006 (no version) [Samuel A. Rebelsky] ; * Created a few versions of tally while writing analyst.scm ; Sunday, 12 November 2006 (v 0.1) [Samuel A. Rebelsky] ; * Created. ; * Sample procedures: filter, tally. ; +-------+---------------------------------------------------------------- ; | To Do | ; +-------+ ; [] Finish documentation. ; [] Try other procedure (binary search, merge sort, ...)