;; From page 22 of book (define member? (lambda (a lat) (cond ((null? lat) #f) (else (or (eq? (car lat) a) (member? a (cdr lat))))))) ;; length written all in one function ;; computes as (+ 1 (+ 1 (+ ... 0))) (define my-length (lambda (lat) (cond ((null? lat) 0) (else (+ 1 (my-length (cdr lat))))))) ;; another way of writing length using a helper function (define length-helper (lambda (lat length-so-far) (cond ((null? lat) length-so-far) (else (length-helper (cdr lat) (+ 1 length-so-far)))))) (define your-length (lambda (lat) (length-helper lat 0))) ;; ---- old stuff from last week (structure.scm) ; constructor to create a part with no subparts. ; called as (simple-part n) where n is the ; name of the part. (define simple-part (lambda (s) (list 'simple-part s))) ; constructor to create a part with subparts ; called as (complex-part n l) where n is the ; name of the part and l is the list of its ; subparts. (define complex-part (lambda (s l) (list 'complex-part s l))) ; test to determine whether a part has no subparts. (define is-simple? (lambda (s) (eq? (car s) 'simple-part))) ; test to determine whether a part has subparts. (define is-complex? (lambda (s) (eq? (car s) 'complex-part))) ; accessor function that returns the name of a part. (define name-of (lambda (s) (car (cdr s)))) ; accessor function that returns the list of all ; the subparts of a part. if the part is simple, ; this returns the empty list. (define parts-of (lambda (s) (if (is-simple? s) '() (car (cdr (cdr s)))))) ; here is a simple example of how to use the constructors ; to create a representation of a whole that is made up ; of parts. (define basic-doric-form (complex-part 'doric-form (list (simple-part 'entablature) (simple-part 'column) (simple-part 'pedestal)))) ; here is a complex example of how to use the constructors ; to create a representation of a whole that is made up of ; parts. (define full-doric-form (complex-part 'doric-form (list (complex-part 'entablature (list (simple-part 'cornice) (simple-part 'frieze) (simple-part 'architrave))) (complex-part 'column (list (complex-part 'capital (list (complex-part 'abacus (list (simple-part 'fillet) (simple-part 'cymation) (simple-part 'plinth))) (complex-part 'necking (list (simple-part 'fillets) (simple-part 'astragal) (simple-part 'cincture))))) (simple-part 'shaft) (simple-part 'column-base))) (complex-part 'pedestal (list (simple-part 'cap) (simple-part 'dado) (simple-part 'base)))))) ;; ---- end of old stuff ; part-names ; given a structure s, ; return the list of names of the parts of s. (define part-names (lambda (s) (part-name-helper (parts-of s)))) ;; give another definition of this function ;; part-names2 ;; using my-map instead of part-name-helper ;; (*** see my-map below) (define part-names2 (lambda (s) (my-map names-of (parts-of s)))) ; part-name-helper ; given a list of structures (s1 ... sk) ; return the list of the names of the structures ; ((name-of s1) ... (name-of sk)). (define part-name-helper (lambda (part-list) (cond ((null? part-list) '()) (else (cons (name-of (car part-list)) (part-name-helper (cdr part-list))))))) ;; a generalized version of part-name-helper is map: (define my-map (lambda (f l) (cond ((null? l) '()) (else (cons (f (car l)) (my-map f (cdr l))))))) ; problem: use my-map to add 1 to each element of '(1 7 11) (my-map add1 '(1 7 11)) ;---> (list 2 8 12) ; problem: use my-map to multiply each element of '(1 7 11) by 7 (define m-by-7 (lambda (a) (* a 7))) (my-map m-by-7 '(1 7 11)) ;---> (list 7 49 77) ; or using an unnamed function (my-map (lambda (a) (* a 7)) '(1 7 11)) ;---> (list 7 49 77)