CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

58
CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    220
  • download

    1

Transcript of CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Page 1: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

CS 3 Final Review

Gilbert Chou, Jenny Franco and Colleen Lewis

December 14, 20081-4pm GPB

Page 2: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Warm-up!

What is your favorite color?

Option A: Brown

Option B: Orange

Option C: Yellow

Option D: Green

Page 3: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the Output

Page 4: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

((repeated bf 3) '(cat dog hat bat)) _____

Option A: Error

Option B: ‘bat

Option C: bat

Option D: (bat)

Page 5: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

((repeated bf 3) '(cat dog hat bat)) _____

Option A: Error

Option B: ‘bat

Option C: bat

Option D: (bat)

• butfirst of a sentence ALWAYS returns a sentence

• Never quote the output of scheme!

• (repeated bf 3) returns a procedure that takes a word or a sentence.

Page 6: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(cons ‘() (list ‘() (cons ‘() ‘()))) _____

Option A: Error

Option B: ( () () (()) )

Option C: ( () () () () )

Option D: ( () () (() ()) )

Page 7: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(cons ‘() (list ‘() (cons ‘() ‘()))) _____

Option A: Error

Option B: ( () () (()) )

Option C: ( () () () () )

Option D: ( () () (() ()) )

• Review what cons, append and list do…

Page 8: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(empty? ‘()) _____

Option A: Error

Option B: #t ;; True

Option C: #f ;; False

Option D: no idea!

Page 9: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(empty? ‘()) _____

Option A: Error

Option B: #t ;; True

Option C: #f ;; False

Option D: no idea!

• empty? returns true for empty sentences and words

• Don’t use it with lists!

Page 10: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(empty? “”) _____

Option A: Error

Option B: #t ;; True

Option C: #f ;; False

Option D: no idea!

Page 11: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(empty? “”) _____

Option A: Error

Option B: #t ;; True

Option C: #f ;; False

Option D: no idea!

• empty? returns true for empty sentences and words

• Don’t use it with lists!

Page 12: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(null? “”) _____

Option A: Error

Option B: #t ;; True

Option C: #f ;; False

Option D: no idea!

Page 13: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(null? “”) _____

Option A: Error

Option B: #t ;; True

Option C: #f ;; False

Option D: no idea!

• null? returns true for null lists

• Don’t use it with words and sentences!

Page 14: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(null? (cons “” ‘())) _____

Option A: Error

Option B: #t ;; True

Option C: #f ;; False

Option D: no idea!

Page 15: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(null? (cons “” ‘())) _____

Option A: Error

Option B: #t ;; True

Option C: #f ;; False

Option D: no idea!

• An empty word counts as something inside the list. So – it is not null?

Page 16: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(assoc ‘x ‘((a 1) (b 2) (x 3) (x 4)) _____

Option A: Error

Option B: 3

Option C: (x 3)

Option D: (x 4)

Page 17: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(assoc ‘x ‘((a 1) (b 2) (x 3) (x 4)) _____

Option A: Error

Option B: 3

Option C: (x 3)

Option D: (x 4)

• assoc just returns the first match.

• Returns the entire pair – not just the car of the cdr

Page 18: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(member ‘x ‘(a b c d x e f g)) _____

Option A: Error

Option B: #t ;; True

Option C: #f ;; False

Option D: something else

Page 19: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(member ‘x ‘(a b c d x e f g)) _____

Option A: Error

Option B: #t ;; True

Option C: #f ;; False

Option D: something else

• Returns: (x e f g)

• member returns the rest of the sentence starting with the thing you were looking for. It is a semi-predicate

• member? returns just #t or #f

Page 20: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(member? ‘x ‘(a b c d x e f g)) _____

Option A: Error

Option B: #t ;; True

Option C: #f ;; False

Option D: something else

Page 21: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(member? ‘x ‘(a b c d x e f g)) _____

Option A: Error

Option B: #t ;; True

Option C: #f ;; False

Option D: something else

• member returns the rest of the sentence starting with the thing you were looking for. It is a semi-predicate

• member? returns just #t or #f

Page 22: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(member? ‘x ‘(a b c d (x) e f g)) _____

Option A: Error

Option B: #t ;; True

Option C: #f ;; False

Option D: something else

Page 23: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(member? ‘x ‘(a b c d (x) e f g)) _____

Option A: Error

Option B: #t ;; True

Option C: #f ;; False

Option D: something else

• member doesn’t look inside of sublists

Page 24: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(cons 1 2) _____

Option A: Error

Option B: (1 2)

Option C: (1 . 2)

Option D: something else

Page 25: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(cons 1 2) _____

Option A: Error

Option B: (1 2)

Option C: (1 . 2)

Option D: something else

• The second argument to cons should be a list otherwise you end up with this weird dot.

• Remember cons pulls out the “underwear” of the second argument and drops the first argument in.

Page 26: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(map cons 1 ‘(2)) _____

Option A: Error

Option B: (1 2)

Option C: (1 . 2)

Option D: ((1 2))

Page 27: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(map cons 1 ‘(2)) _____

Option A: Error

Option B: (1 2)

Option C: (1 . 2)

Option D: ((1 2))

• Map takes in a procedure and one or more LISTS!

• There should be the same number of lists as there are arguments that the procedure takes in!

Page 28: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(map cons ‘(1) ‘(2)) _____

Option A: Error

Option B: (1 2)

Option C: (1 . 2)

Option D: ((1 2))

Page 29: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(map cons ‘(1) ‘(2)) _____

Option A: Error

Option B: (1 2)

Option C: (1 . 2)

Option D: ((1 . 2))

• Map takes the car of each list and calls the procedure cons with them. i.e. (cons 1 2) it does that for each element in the lists and puts the result in a list!

Page 30: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Fill in the blank

__________________ ((1 2) (A B))

Option A: (map cons ‘(1 A) ‘((2) (B)))

Option B: (map cons ‘((1) (A)) ‘((2) (B)))

Option C: (map cons ‘(1 A) ‘(2 B))

Option D: something else

Page 31: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Fill in the blank

__________________ ‘((1 2) (A B))

Option A: (map cons ‘(1 A) ‘((2) (B)))

Option B: (map cons ‘((1) (A)) ‘((2) (B)))

Option C: (map cons ‘(1 A) ‘(2 B))

Option D: something else

• (list (cons 1 ‘(2)) (cons A ‘(B)))

• (list (cons ‘(1) ‘(2)) (cons ‘(A) ‘(B)))

• (list (cons 1 2) (cons A B))

Page 32: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(=‘x ‘x) _____

Option A: Error

Option B: #t ;; True

Option C: #f ;; False

Option D: something else

Page 33: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(=‘x ‘x) _____

Option A: Error

Option B: #t ;; True

Option C: #f ;; False

Option D: something else

• = only works with numbers! For words/sentences use equal?

Page 34: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(=‘1 1) _____

Option A: Error

Option B: #t ;; True

Option C: #f ;; False

Option D: something else

Page 35: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(=‘1 1) _____

Option A: Error

Option B: #t ;; True

Option C: #f ;; False

Option D: something else

• Numbers are words!

• The quote is implicit on numbers

Page 36: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(every first ‘awesome) _____

Option A: Error

Option B: a

Option C: awesome

Option D: (a w e s o m e)

Page 37: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Predict the output

(every first ‘awesome) _____

Option A: Error

Option B: a

Option C: awesome

Option D: (a w e s o m e)

• Every returns a sentence even if it is given a word!

Page 38: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

1 (define (depth lst)

2  (if (not (list? lst))

3       0

4       (max (map (+ depth 1) lst))))

Option A: Error

Option B: 0

Option C: 1

Option D: something else

(depth 1234567) ________

Page 39: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

1 (define (depth lst)

2  (if (not (list? lst))

3       0

4       (max (map (+ depth 1) lst))))

Option A: Error

Option B: 0

Option C: 1

Option D: something else

(depth 1234567) ________

Page 40: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

1 (define (depth lst)

2  (if (not (list? lst))

3       0

4       (max (map (+ depth 1) lst))))

Option A: Error

Option B: 0

Option C: 1

Option D: something else

(depth ‘((a)(b))) ________

Page 41: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

1 (define (depth lst)

2  (if (not (list? lst))

3       0

4       (max (map (+ depth 1) lst))))

Option A: Error

Option B: 0

Option C: 1

Option D: something else

(depth ‘((a)(b))) ________

Page 42: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

1 (define (depth lst)

2  (if (not (list? lst))

3       0

4       (max (map (+ depth 1) lst))))

Option A: 1

Option B: 2

Option C: 3

Option D: 4

Which line has an error? ________

Page 43: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

1 (define (depth lst)

2  (if (not (list? lst))

3       0

4       (max (map (+ depth 1) lst))))

Option A: 1

Option B: 2

Option C: 3

Option D: 4

Which line has an error? ________

Page 44: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

1 (define (depth lst)

2  (if (not (list? lst))

3       0

4       (max (map (+ depth 1) lst))))

Correct the error!

_____________________________

Page 45: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

1 (define (depth lst)2  (if (not (list? lst))3       04       (max (map (+ depth 1) lst))))

New line 4:(+ 1 (reduce max (map depth lst)))

Page 46: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

List or Sentence?

Page 47: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Would you use this with Lists or Sentences?

itemOption A: lists

Option B: sentences

Option C: neither

Option D: both

Page 48: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Would you use this with Lists or Sentences?

itemOption A: lists

Option B: sentences

Option C: neither

Option D: both

• list-ref for lists

• item for sentences/words

Page 49: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Would you use this with Lists or Sentences?

assocOption A: lists

Option B: sentences

Option C: neither

Option D: both

Page 50: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Would you use this with Lists or Sentences?

assocOption A: lists

Option B: sentences

Option C: neither

Option D: both

• Assoc lists are LISTS

Page 51: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Would you use this with Lists or Sentences?

firstOption A: lists

Option B: sentences

Option C: neither

Option D: both

Page 52: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Would you use this with Lists or Sentences?

firstOption A: lists

Option B: sentences

Option C: neither

Option D: both• car for lists

• first for sentences/words

Page 53: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Would you use this with Lists or Sentences?

reduceOption A: lists

Option B: sentences

Option C: neither

Option D: both

Page 54: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Would you use this with Lists or Sentences?

reduceOption A: lists

Option B: sentences

Option C: neither

Option D: both• reduce for lists

• accumulate for sentences/words

Page 55: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Would you use this with Lists or Sentences?

mapOption A: lists

Option B: sentences

Option C: neither

Option D: both

Page 56: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Would you use this with Lists or Sentences?

mapOption A: lists

Option B: sentences

Option C: neither

Option D: both• map for lists (and it is way cooler

than every!)

• every for sentences/words

Page 57: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Would you use this with Lists or Sentences?

filterOption A: lists

Option B: sentences

Option C: neither

Option D: both

Page 58: CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, 2008 1-4pm GPB.

Would you use this with Lists or Sentences?

filterOption A: lists

Option B: sentences

Option C: neither

Option D: both• filter for lists

• keep for sentences/words