2015-11-04

posted Nov 4, 2015, 4:55 AM by Samuel Konstantinovich   [ updated Nov 4, 2015, 6:21 AM ]
A. Do Now/mini Lab: (reverseL + reverseLS)
;Consider this incomplete reverseList:

(define (reverseL L)
  (cond
    ((null? L) L)
    (else             ???)))

;How do we use append to write reverse?

;Hint for people that had problems:
;Conceptually, pretend there is a built in function that does what you want this function to do, except not on the entire list... that function is called (reverseL  L)  ... yay recursion!

More Hints:
;1 Given a list L, how would you use the functions below to create L in reverse?      ;(car x)    ;(cdr x)   ;(reverseL x) ; 
;complete reverseL!

(reverseL  '(1 2 9 5 0) )  -> (0 5 9 2 1)
(reverseL  '(1 2 (9 5) 0) )  -> (0 (9 5) 2 1)

;Test your code before moving on:

;2. BEFORE writing the solution for number 2, write on paper:
-a In GENERAL where do you need to make new cases when modifying a function to work with sublists?
-b Where in your reverseL function you need to make changes to handle sublists?

Complete the function (reverseLS  L) that not only reverses the top level 
;elements, but will go inside sublists and reverse those as well.
(reverseLS  '(1 2 9 5 0) )  -> (0 5 9 2 1)
(reverseLS  '(1 2 (9 5) 0) )  -> (0 (5 9) 2 1)
(reverseLS  '(1 (2 (9 5)) 0) )  -> (0 ((5 9) 2) 1)

;3. Write a function (myMap f L) take any single parameter function (f x) and list of numbers L as parameters.
;Apply f to each element of L, and return a list of all the changed values.
;f must be a function that takes a single number, and returns a number, like (sqrt x), (addOneTo x), (cutInHalf x)

e.g.
(define f (lambda (x) 
    (+ x 1)))
(define L '( 1 9 3 4) )
(myMap f L) ;returns (2 10 4 5)

(myMap sqrt '(0 1 4 9) ) ;returns ( 0 1 2 3)


hint:
;we know that f takes a single number.
;we know that L is a list of numbers.
(define (myMap f L)
   ??????
)



;cool thing to note
;you can define functions on the fly:
(map 
   (lambda (x) (+ x 1))  
   '(0 1 4 9) ) 
;this returns ( 1 2 5 10)










practice problems if you don't score as well as you like:
;P0
;Write a function (eliminateNegatives L)  takes a list of real numbers. It should return a list with only the non-negative values. Try first without sublists, then add sublists.
(eliminateNegatives (list 2 -3 4 0 5 -3 10) ) ;returns  (2 4 0 5 10)
(eliminateNegatives '( (2 -3) 4 0 5 -3 10) ) ;returns  ( ( 2 ) 4 0 5 10)

;P1
;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:  (verify on a computer)
(what '( 12 (14 ( 2) ) 3))

;P2 Evaluate:
(length '((1)(2)(3)))
(length '(()()()))
(length '((1 2) 3))
(length '((1 2 3)))
(length '((1 2 (3))))
(length '((1 2 3) 4))

;P3 Write an expression using a sequence of car/cdr to extract 
;the value 3 from each of the following 3 lists:
 (define super '(1 2 3))
 (define fighting '((1 2 (3 4)(5 6))))
 (define robot '( (1 (2 (3) 4)))) 

Comments