10-08-2013 - LAB

posted Oct 8, 2013, 5:39 AM by Samuel Konstantinovich   [ updated Oct 8, 2013, 7:31 AM ]
Goal: More car and cdr.

Do Now:
1. (car (cdr (cdr '("Applejack" "Fluttershy" "Twilight" "Sparkle") ) ) )
2. (car (cdr (cdr (cdr '(9 0 2 1 0) ) ) ) )
3. (cdr (cdr (cdr '(8 6 7 5 3 0 9) ) ) )
4. (cdr (cdr (cdr (cdr '(sparkle fairies arent vampires) ) ) ) )

New Stuff:
There is a shortcut for a long string of car and cdr:

(define x '( 9 2 7 5 1) )

(car ( cdr (cdr x ) ) )  -> 7
is the same as:
(caddr x) -> 7

So (cxyzwr x) is equivalant to (cxr ( cyr (czr (cwr x) ) ) )
Where x y z and w are either a or d.

Note: This is limited to 4x car/cdr in drRacket. You can't do cadddddddr for example, because there are too many a's and d's.

Lab BasicLists: (You must have a working copy on your account when I decide to go check what labs you have)
1. Write a function (second x) that accepts a list as a parameter, and returns the 2nd element. 
1b. Modify your function return the 2nd element when there are at least 2 elements, or return false if there aren't enough elements.
e.g.
(second '( a b c) ) -> b
(second '( 1 ) ) -> #f

2. Write a function (third x) that accepts a list as a parameter, and returns the 3rd element. 
2b. Modify your function return the 3rd element when there are at least 3 elements, or return false if there aren't enough elements.
(third '( a b c) ) -> c
(third '( a b ) ) -> #f


3. Make a function (sumList3 x) that accepts a list of 3 integers, and returns the sum of the integers. It returns 0 when the list has a different number of elements. You can assume the elements are integers. 
(sumList3 '(1 9 2) ) -> 12
(sumList3 '(12 13 14 15) ) -> 0

Comments