2014-10-28

posted Oct 28, 2014, 7:09 AM by Samuel Konstantinovich   [ updated Oct 28, 2014, 7:09 AM ]
;HOMEWORK: You need this to work in class tomorrow:
;Write the function (listReverse L) that takes a list and returns 
;all of the top level elements of that list in the reverse order.
;(listReverse '(1 (2 3) 4 5)) ->  (5 4 (2 3) 1)



;DO NOW ON PAPER
;write a function that counts the number of sublists in a list, 
;including sublists
(countSubs '( 1 (2 3) (4 5) )) ;-> 2
(countSubs '( 1 ((2 3) (4 5)) )) ;-> 3
(countSubs '( (1) ((2) 3) (4 5) )) ;-> 4
;first: Write sentences that identifies: 
;       a) the base case(s)
;       b) when you need to call countSubs again.
;(write these down before writing down your code)







(define (hey? L)
  (cond
  ( (null? L) L)
  ( (list? (car L))  (hey? (append (car L) (cdr L))))
  ( else             (cons (car L) (hey? (cdr L))))))
;What does this do to a list with no sublists?
;(hey? '( a b c))
;What about when there is a sublist?
(hey? '( (1 2) 9) )
(hey? '( (1 2) 9 (3 4)))
(hey? '( 1 (2 (9)) (3 4)))




Comments