Courses‎ > ‎Introduction to Computer Science 1‎ > ‎Konstantinovich‎ > ‎ML1 9/2012‎ > ‎labs‎ > ‎

Lab 06

posted Oct 16, 2012, 6:15 PM by Samuel Konstantinovich   [ updated Dec 21, 2012, 5:25 AM ]

;I truly apologize for being absent this long. The only thing
;I can do to help you all is give you these little labs with 
;a small amount of new material so you learn something even
;when I am not there. That being said, only 2 people asked me
;questions about the work. So I will assume that if I were to 
;quiz you on lists when I return... you would all understand everything!
;(or at least try to do so now that I warned you)

;Computer science is more about reading than Mathematics is. You 
;never get a problem so straight forward as "Solve the following
;equation*" in a computer science class. Read this whole lab carefully
;and if you didn't read the previous ones carefully, I urge you 
;to go back and re-check them. 

;*To be fair, there are almost no such questions when you go to 
;college to study mathematics either

;Hint: I won't quiz you the day I come back... 
;but the day after is fair game. 

;LAB 6

;If you saw the long introduction and thought "tldr" then you should
;rethink your work ethic. I guarantee lazy makes you lose points in 
;my class. 

;Lets work with lists of numbers for now:

;DO NOW: (You can work with others but should try at least one on your own)

;Part 1 Function traces:

;For EACH of the 2 mystery functions:
;1. Trace through them both on paper using any list of numbers
;2. Then see if the result you got is the same as the function
;3. If not trace again, you can compare with others... 
;4. What is this function supposed to be named?

(define (MST3K L)
    ((null? L) 0)
    (else (+ (car L) (MST3K (cdr L))))))

(define (MSTRE L)
    ((null? L) 0)
    ((even? (car L))(MSTRE (cdr L)))
    (else (+ (car L) (MSTRE (cdr L))))))

;Part 2: evaluation with car and cdr

;Evaluate each of these without running them in racket:
;1 (car (cdr (cdr '( a b c d e f))))
;2 (cadddr '( a b c d e f))
;3 (caddddr '( a b c d e f))
;Check your answers in drRacket.

;Notice that c____r shortcuts (cddr L) or (caddr L) 
;only works with up to 4 a/d's inside. This is still 
;a useful shortcut however!!!
;(cdr (cddddr 
;(cddr (cddddr ... etc. 

;OK! Now try 4
;4. (car (car (cdr '( a b c d e f))))
;a. What must car and cdr be used on? 
;b. Can you use car on the results of a car?
;c. When would it be possible to use car twice? (hint: listCeption)


;I promise I will briefly go over these commands, but I strongly 
;urge you to copy these into drRacket and see what they do.
;You should modify them, and experiment to confirm/contradict your
;assumptions. Test them a little and you will learn more I promise. 

;(cons value list) -> list with value added as the 1st element.
;(cons 3 '(2 6)) -> (3 2 6)
;(cons 1 '()) -> (1)

;Semi strange:
;(define x 1)
;Compare the next two carefully:
;(cons x '( 2 3)) -> (1 2 3)
;(cons 'x '( 2 3)) -> (x 2 3)
;Explain to your neighbor why that happened. Confirm with a different neighbor. 

;Now is where the weird things happen:
;(cons '(1 2) '(3 4) ) -> ((1 2) 3 4)  
;the list got put at the start of the 2nd list

;(append list1 list2) -> the combination of both lists in order
;(append '(4 5 6) '(1 2 3)) -> (4 5 6 1 2 3) 
;the numbers stay in order , they don't change to the order you 
;may have expected. 

;Play with cons and append.
;Your goal should be to beable to verbalize to me how they are different/similar.
;You can try writing a function that tries to cons a bunch of numbers to a list
;then try the same thing with cons, and realize that:
;append requires two lists...
;cons can take   anything + 1 list:   such as list and a list, or integer and a list
;so you cant (cons 3 L), you need to (cons '(3) L)

;how would you cons an integer x to a list?
;(cons '(x) L)  would put the LETTER x in the list.
;We can use the (list x) command
;(list 3) -> (3)
;(define x 3)
;(list x) -> ( THEVALUEOFX )  ;so if x was 3, that would be (3)