2014-10-31 - HW19

posted Oct 30, 2014, 9:02 PM by Samuel Konstantinovich   [ updated Oct 31, 2014, 8:47 PM ]
Goal: Evil Computer Science

Homework19:
Complete the Written work you started in class. (Handwritten is fine!)
Print out :  (Handwritten is NOT fine!)
a. The Coding work after you write and test it in drracket. 
b. Three test cases per problem and the results of the test cases. 
Don't forget your heading. If you handwrite your name, I will not take it.

Written Work:

1. Given:
(define L1 '( a b) )
(define L2 '( c d) )
What is the simplest way to use L1 and L2 with list 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?

For Problems 5-8 Answer the question AND explain why!
5. When you have a cond statement that looks at the car or cdr of the list in some of the booleans, when (Chronologically in your cond) should you check to see if the list is null? 
6.  When counting how many of a specific element can be found inside of a list (such as countEmptyListsInList, or countTopLevelFivesInList), what is a good base case to use?
7.  When counting how many of a specific type of digit can be found inside of an integer(like countEvenDigits or countPrimeDigits), what is a good base case to use?
8.  What are the two parts required in EVERY recursive function?


Coding Work:
1. (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) ) ->  (2 4 0 5 10)
(eliminateNegatives '( (2 -3) 4 0 5 -3 10) ) ->  ( ( 2 ) 4 0 5 10)

2.
(map f L)   take any 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.

e.g.
(define f (lambda (x) (+ x 1)))
(define L '( 1 9 3 4) )
(map f L) -> (2 10 4 5)

(map sqrt '(0 1 4 9) ) -> ( 0 1 2 3)
(map (lambda (x) (+ x 1))  '(0 1 4 9) ) -> ( 1 2 5 10)

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

)


Comments