Lecture: 10-26-2012

posted Oct 26, 2012, 5:58 AM by Samuel Konstantinovich   [ updated Nov 20, 2012, 5:44 AM ]
;Remember that yesterday's new topic was functions
;that process lists that have sublists today we will continue
;with sublist processing.
;DO NOW : Trace Through this function call:

;Trace 1.
(define (who L)
  (cond
    ((null? L) L)
    ((list? (car L)) (append (who (car L))(who (cdr L))))
    (else  (cons (car L) (who (cdr L))))))
;Trace Through the function call:
(who '(3 ( 4 32 ) 99))
    
;Some of the functions from problems (3-7) will be due next week. 
;You should work on them this weekend for practice, and so you get
;some of the work done ahead of time. I will go over and post some
;of the solutions, the others must be submitted. 

;Now try to write these functions:
;3. Count the number of positive integers in a list (including it's sublists)
;    (countPositive '( (2 -3) -4 (3 (-1) 0 2) )  -> 3    
;
;4. Count the number of sublists in a list (including it's sublists)
;    (countLists '( (2 3) (3 (5) 2) )  -> 3    
;    Think how many open parentheis are in the list...
;
;5. Remove the 0's from a list of integers which may have sublists.
;    (stripZeros '( 0 2 (3 0 (0 ) ) 5))   -> ( 2 ( 3 () ) 5)
;
;6. Add 1 to each of the values in a list of integers which may have sublists.
;    (addOne '( 9 9 (4 3) (2 (1) ) ) )   -> (10 10  (5 4) (3 ( 2 ) ) ) 
;
;7. Reverse a list, including sublists 
;   (myReverse '( a b c ) )   -> (c b a)
;   (myReverse '( 1 2 (3 4) (5 6))) -> ((6 5)(4 3) 2 1)


;Trace 2. (PRACTICE FOR HOME)
;Assume L cannot be an empty list or contain any empty lists
(define (what L)
  (cond
    ;First two cases process sublist
    ((and (list? (car L)) (null?(cdr L)))   (what (car L)))
    ((list? (car L))   (max (what (car L)) (what (cdr L))))
    ;second two cases process non-sublists
    ((null?(cdr L))   (car L)) 
    (else      (max (car L) (what (cdr L))))))
;Trace through the following:
(what '( 12 (14 ( 2) ) 3))



Comments