2015-10-23 HW12

posted Oct 23, 2015, 4:58 AM by Samuel Konstantinovich   [ updated Oct 24, 2015, 4:19 AM ]
Goal: More list recursion

Do Now: 
New Method: (list? x)  is true when x is a list, false otherwise.

Answer this: What does the 2nd case in the cond statement allow us to do in future programs?

(define MrE (lambda (L) 
              (cond 
                ( (null? L)       0 )
                ( (list? (car L)) (+ (MrE (car L))(MrE (cdr L))) )
                (  else           (+ (car L) (MrE (cdr L))) )
                )))
;Expand and Evaluate:
(MrE '( 100 ( 1 ) (10 4) ) )

Homework:  [update]
1. Think of what your costume will be for next Friday. Halloween is important!
2. Look at the solution to HW11 on piazza, it will give you some good ideas for future functions!
3. Complete this Homework by Tuesday October 27th

Lab/HW12

1. Write the function (sumOfEvenPositive L) -> returns the sum of all positive even values of list L, which contains 0 or more real numbers, and only atoms.

2. Write the function (maxValue L) -> returns the largest value of a list L which contains only atoms, only real numbers, and has at least one element. 
You may use the max function. Only use the max function on 2 values at a time. 

3. Write the function (maxValueSub L) -> Returns the largest value of a list L which contains only real numbers, has at least one number in it, and MAY contain sublists(non-empty also) . Only use the max function on 2 values at a time. 

Non-empty lists and Non-empty sublists. Otherwise you need an empty list base case too.

There are actually 2 base cases:

((null? (cdr L)) (car L)) only works when the car is an atom.

What if the car is a list, and the only element?


4. Write a function that counts the number of top level lists in a list
;first: Write sentences that identifies the base case(s)
(countSubs '( 1 (2 3)   (4 5) )) ;-> 2
(countSubs '( 1 (  (2 3) (4 5)  ) )) ;-> 1 because it doesnt go into the sublist to count the sub-sub lists
(countSubs '( (1)   ((2) 3)    (4 5) )) ;-> 3


5. Write a function that counts the number of sublists in a list, AND includes looking in sublists (look at todays DoNow for hints)
(countSubsSub '( 1 (2 3) (4 5) )) ;-> 2
(countSubsSub '( 1 ((2 3) (4 5)) )) ;-> 3
(countSubsSub '( (1) ((2) 3) (4 5) )) ;-> 4
;first: Write sentences that identify: 
;       a) the base case(s)
;       b) when you need to call countSubs again.
;(write these down before writing down your code)

Comments