CS 22

Clab 10: map and filter


First, do exercises 2.30, 2.31, 2.32 on pages 112-113. You can find them by scrolling down a couple of screens from: http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-15.html#%_sec_2.2.2
If necessary review the material on pages 105 and 112. Remember to substitute '() for nil wherever the text uses nil or put (define nil '()) in your definitions window.

Here is the procedure filter from page 115 of the text.

(define filter (lambda (pred? l) (cond ((null? l) '()) ((pred? (car l)) (cons (car l) (filter pred? (cdr l)))) (else (filter pred? (cdr l)))))) Use filter to define a procedure remove-neg that given an input list of integers, returns a list of all the non-negative integers in the input list. > (remove-neg '(1 -3 4 5 -6 -7)) (1 4 5)

Now a challange

Finish the 8-queens problem, ex 2.42 page 124-126. You can find it by scrolling up a couple of screens from: http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-15.html#%_sec_2.2.4


Solutions for Clab9 functions. Here is some code for deep versions of subst and remove-all: ;; subst* returns a list that substitutes new for old in an arbitrary ;; list, l, of symbols: (define subst* (lambda (old new l) (cond ((null? l) '()) ((list? (car l)) (cons (subst* old new (car l)) (subst* old new (cdr l)))) ((eq? (car l) old) (cons new (subst* old new (cdr l)))) (else (cons (car l) (subst* old new (cdr l))))))) ;; remove-all* removes all occurrences of sym from an arbitrarily ;; nested list: (define remove-all* (lambda (sym l) (cond ((null? l) '()) ((list? (car l)) (cons (remove-all* sym (car l)) (remove-all* sym (cdr l)))) ((eq? (car l) sym) (remove-all* sym (cdr l))) (else (cons (car l) (remove-all* sym (cdr l)))))))

If you have any questions from last clab, begin with it and get your questions answered.

ask any questions you may have.