2015-11-01 ExamFriday

posted Nov 2, 2015, 4:49 AM by Samuel Konstantinovich   [ updated Nov 2, 2015, 7:03 AM ]
EXAM FRIDAY~! (All scheme content so far up to Monday.)

Do Now:
Given two lists L1 and L2
(define L1 '( a b) )
(define L2 '( c d) )
1. What is the simplest way to use L1 and L2 with list/cons/append functions to create each of the following?
  a. ( ( a b) c d)
  b. ( a b c d)
  c. ( a b (c d) )

2. What two functions are required to split a list into its two parts?
3. What two functions are required to split apart an integer into a single digit, and the rest of the number?
4. What boolean would you use to check for a sublist in a list?


Classwork:
Together:
List reverse from before.
List reverse using append.

LabMakeAtomsTopLevel / complete for HW:
1. Write a function (hasSublists? L) that returns true when any of the top level elements of L are lists. 

(hasSublists? '( 2 3 4))  -> #f
(hasSublists? '( 2 (3) 4)) -> #t
(hasSublists? '( )) -> #f
(hasSublists? '( () )) -> #t

2. Write a function (countEmpty L) that counts the number of empty lists are contained in a list L which may have sublists.
(countEmpty '( 3 2  ( ) ( ) ) is 2
(countEmpty '( 2 (9 ( ) ( ) ) ( ) ) ) is 3
(countEmpty '() ) is 0

3. Write a function (makeAllAtomsTopLevel L) that changes a list with sublists, into a list without sublists, with all atoms in the same order as before. 
(makeAllAtomsTopLevel '( 1 2 3) )  is   (1 2 3)
(makeAllAtomsTopLevel '( 1 ( ) ( 2 3 ) ) )  is   (1 2 3)
(makeAllAtomsTopLevel '( 1 ( 2 ( 3) ) ) )  is   (1 2 3)
(makeAllAtomsTopLevel '( (1 2( 9 2 ( (5) 3) ) ) ) )  is   (1 2 9 2 5 3)

Comments