; Abby Walker ; Homework 2 ; Problem 1 ; returns a list containing n copies of x. (define duple (lambda (n x) (cond ((equal? n 0) '()) (else (cons x (duple (- n 1) x)))))) ; Problem 2 ; where lst is a list of 2-lists, returns a list with each 2-list reversed. (define cons-to-end (lambda (dexp lst) (cond ((null? lst) (cons dexp '())) (else (cons (car lst) (cons-to-end dexp (cdr lst))))))) (define reverse (lambda (s) (cond ((null? s) '()) (else (cons-to-end (car s) (reverse (cdr s))))))) (define invert (lambda (lst) (cond ((null? lst) '()) (else (cons (reverse (car lst)) (invert (cdr lst))))))) ; Problem 3 ; returns a zero-based index of the first occurence of s in los, or -1 if there ; is no occurrence of s in los. (define list-index (lambda (s los) (cond ((null? los) -1) ((equal? s (car los)) 0) (else (+ 1 (list-index s (cdr los))))))) ; Problem 4 ; where p is a predicate, returns the list of those elements in lst that ; satisfy the predicate. (define filter-in (lambda (p lst) (cond ((null? lst) '()) ((equal? #t (p (car lst))) (cons (car lst) (filter-in p (cdr lst)))) (else (filter-in p (cdr lst)))))) ; Problem 5 ; returns a list of 2-lists that represents the Cartesian product of los1 ; and los2. the 2-list may appear in any order. (define smallproduct (lambda (a los2) (cond ((null? los2) '()) (else (cons (cons a (car los2)) (smallproduct a (cdr los2))))))) (define product (lambda (los1 los2) (cond ((null? los1) '()) (else (cons (smallproduct (car los1) los2) (product (cdr los1) los2)))))) ; Problem 6 ; returns a list the same as slst, but with all ovvurrences of s1 ; replaced by s2 and all occurrences of s2 replaced by s1. (define swapper (lambda (s1 s2 slst) (cond ((null? slst) '()) ((equal? s1 (car slst)) (cons s2 (swapper s1 s2 (cdr slst)))) ((equal? s2 (car slst)) (cons s1 (swapper s1 s2 (cdr slst)))) (else (cons (car slst) (swapper s1 s2 (cdr slst))))))) ; Problem 7 ; where p1, ..., pn is a sequence of zero or more procedures of one ; argument, returns the composition of all the procedures. The ; composition of zero procedures is the identity procedure, the ; composition of one procedure is the procedure itself, and the ; composition of two or more procedures is specified by: ((compose p1 ; p2 ..) x) -> (p1 ((compose p2 ..) x)) (define compose (lambda lst (cond ((null? lst) (lambda (x) x)) ((null? (cdr lst)) (lambda (x) ((car lst) x))) (else (lambda (x) ((car lst) ((apply compose (cdr lst)) x))))))) ; Problem 8 ; Problem 5 from Problem set 10 (all three parts), on pages 208-209 (define modify (lambda (f old new r) (cond ((null? r) '()) ((equal? (car r) old) (f old new (rest r))) (else (cons (car r) (modify f old new (rest r))))))) ; a) (define L (lambda (old new r) (cons new (cons old r)))) (define insertl (lambda (old new r) (modify L old new r))) ; b) (define ST (lambda (old new r) (cons new r))) (define subst (lambda (old new r) (modify ST old new r))) ; c) (define RM (lambda (old new r) r)) (define rember (lambda (a r) (modify RM a '() r)))