430 likes | 786 Views
CS1 Final Exam Review By Rebecca Schulman December 4, 2002 Quick Overview Topics from the first part of term will not be explicitly covered on the exam, but if you do not understand this material, you will have trouble with the exam Substitution model Standard vs. Special Forms
E N D
CS1 Final Exam Review By Rebecca Schulman December 4, 2002
Quick Overview • Topics from the first part of term will not be explicitly covered on the exam, but if you do not understand this material, you will have trouble with the exam • Substitution model • Standard vs. Special Forms • Higher order procedures • Asymptotic Complexity
Make sure you understand the topics from the second half of the course. These include: • Data structures: lists, trees, and data processing • Message Passing • Operations with Mutation • Environment Diagrams • Tagged Data
Exam Structure • You will have to answer one question on each major concept. • There will be two “tracks” on the exam, so if you do both, your score will be the minimum of your best score for each concept
The Substitution Model Three steps: • Evaluate the operands • Evaluate the operator • Apply the operator to the operands
Substitution and Mutation • We did not get rid of the substitution model when we introduced mutation. But we did make an important change: • We do not substitute the value for parameters into an expression when we apply. Instead, when they are needed, we look up the value of a parameter in the environment
More Substitution and Mutation • Remember also that begin statements mustbe evaluated in left to right order • These type of expressions include the body of begin clauses, but also the consequent portion of cond statements and the body of let expressions
Special Forms • Some scheme expressions do not obey the substitution model. They require operands to be evaluated in a certain order, or require some operands to not be evaluated at all before application
A List of Special Forms • define: Evaluate only the second operand, and associate its value with the first operand, which should be a variable • if : Evaluate the predicate and only evaluate the clause pertaining to whether the predicate is true or false • cond: Evaluate only the predicates, until a true one is found, and then return the value of the consequent expression that matches
More Special Forms • let: Evaluate the values in the binding section and associate them with their corresponding variables inside the let environment • quote: The result of the expression is a symbol with the name given as the sole argument • set!: Only evaluate the second expression, and change the value associated with the variable to be this result
Asymptotic Complexity • We use O notation to talk about approximately how long it will take a procedure to complete • No formal methods are required for CS1: just an informal counting should be enough • Example run times we saw were O(n), O(n2), O(n log n) and O(2n)
Symbols • We can represent a name using symbols in Scheme. A symbol is created using the quote special form bob => error: Unbound variable bob (quote bob) => bob • We abbreviate quote with the ‘ character ‘bob => bob
cons pairs • cons pastes together two elements. By using it recursively, we can create lists, trees and any other data structure we might like • For example, a list of 1,2, and 3 would be: (cons 1 (cons 2 (cons 3 nil)))
Box and Pointer Diagrams • We illustrate cons pairs using a pair of boxes. Each box points to its contents • cons cells can point to numbers, symbols, or other cons pairs, among other things
List processing In class we talked about algorithms for • Adding items from a list • Removing items from a list • Searching for items in a list • Doing the above with and without mutation
Data Representation • Once we have the ability to represent collections of things, we’re left with the obvious question of how to organize it. • We spent the next several weeks in CS1 thinking about several ways to do this.
Abstraction Barriers • The simplest technique we discussed is separating the representation of the data from its meaning • This requires creating an explicit representation, and creating procedures that can interact with the data by creating it and accessing it by meaning, rather than by structure
Tagged Data • We used tagged data in order to label numbers so that they had meaning. For example: (make-dollars 40) => (dollars . 40) (make-pounds 100) => (pounds . 100)
Generic Operations • We then extended this idea to being able to do operations on data in different units (add-money (make-pounds 100) (make-dollars 40)) => (dollars . 196.7) (as of today)
Message Passing • Message Passing allowed us to encapsulate data and operations into the same object
Mutation • Mutation is the ability to change the value of a variable, once it has been defined. This is something that we don’t do in math, and it caused us to make our old substitution model more complicated
The Environment Model • We extended the substitution model by no longer using substitution to associate variables with their value, but by creating a set of environments • We still evaluate and apply procedures in the environment model but we introduced two new concepts
Environment Model (2) • Procedures are explicitly created in the environment model, and are evaluated in a particular environment that captures their local state • Instead of substituting variables at the time of application, we look each one up the environment as needed
Environment Model Rules • Binding variables: Bind simple variables in the current environment • Procedures are created when they are defined. They form a pair, one of which points to its body, the other points to the enviroment where the procedure is created
Environment Model Rules (2) • Applying a procedure creates a new environment in which the parameters of the procedure are bound to the operand • set! changes the value of the variable that is referred to in the current environment
Procedures as Local State • The first way we used mutation was to create procedures that hold state. We did this by creating procedures that had their own environment. For example: (define (make-accum initial) (let ((valueinitial)) (lambda (change) (set!value (+valuechange)) value)))
Mutation of Data • The other way we learned how to do mutation was to change data structures • We did this with set-car! and set-cdr! • set-car! points the first part of a cons pair to the same thing pointed to by its second argument, and set-cdr! does the same to the cdr part of the cons pair
eq? and equal?: When is the whole not the sum of its parts? • eq? tests whether the two whole objects are the same • equal? tests whether the parts of two data structures are all the same • Two objects that can be equal but not eq would be: • (define a ‘(1 2 3)) • (define b ‘(1 2 3))
Extended Example: Gambling • We’ll go on an extended exercise to pit your scheme skills against the casinos and try not to lose all of your money…
A Blackjack Game You decide to test your strategy ideas in simulation first to see how much money you will win or lose. Dividing this into a few tasks we will: • Build a message-passing deck of cards • Build a few blackjack players • And a table, that will simulate the playing of many games
The Deck of Cards • We begin with the ranks and suits (definesuits '(clubs diamonds hearts spades)) (defineranks '(A 2 3 4 5 6 7 8 9 10 J Q K)) The deck of cards are simply all combinations of these
List processing: all-combinations (define (all-combinations proc first second) (define (helper abcurrent) (cond ((null? a) current) ((null? (cdrb)) (helper (cdra) second (cons (proc (cara) (carb)) current))) (else (helper a (cdrb) (cons (proc (cara) (carb)) current))))) (helper first second (list)))
A deck of cards • A deck of cards is simply the this applied to a single make-card procedure (define deck-of-cards (all-combinations make-card suits ranks)) (define (decks-of-cards n) (if (= n 0) (list) (append deck-of-cards (decks-of-cards (- n 1)))))
Shuffling • Shuffling cards can be reduced to transposing each element so that it ends up in a random position. We’ll do this functionally, to show some more list processing techniques
(define (transpose-two-elements lst x y) (let ((xth (nth-element lst x)) (yth (nth-element lst y))) (define (transpose-helper current n result) (cond ((null? current) result) ((= n x) (transpose-helper (cdr current) (+ n 1) (append result (list yth)))) ((= n y) (transpose-helper (cdr current) (+ n 1) (append result (list xth)))) (else (transpose-helper (cdr current) (+ n 1) (append result (list (car current))))))) (transpose-helper lst 1 (list))))
Shuffling (2) Shuffling is simple now that we have the transposition procedure: (define (shuffle list-of-cards) (define (shuffle-helper position current-cards) (if (= position 0) current-cards (let ((other-element (random-1-to-n position))) (shuffle-helper (- position 1) (transpose-two-elements current-cards position other-element))))) (shuffle-helper (length list-of-cards) list-of-cards))
The Deck (Message Passing) (define (make-decks deck-count) (let ((deck (shuffle (decks-of-cards deck-count)))) (define (draw-cards n result) (if (= n 0) result (begin (let ((next (cons (car deck) result))) (set! deck (cdr deck)) (draw-cards (- n 1) next))))) (define (enough-cards deck arg) (>= (length deck) arg)) (lambda (message arg) (cond ((eq? message 'draw) (if (not (enough-cards deck arg)) (set! deck (shuffle (decks-of-cards deck-count)))) (draw-cards arg (list)))))))
The Blackjack Dealer (define (blackjack-dealer deck) (define (hit current-hand) (if (>= (score current-hand) 17) current-hand) (hit (append (deck 'draw 1) current-hand)))) (hit (list)))
The Blackjack Player (define (good-player deck dealers-card) (define (hit current-hand) (cond ((> (score current-hand) 17) current-hand) ((< (score current-hand) 12) (hit (append (deck 'draw 1) current-hand))) ((memq (score current-hand) '(13 14 15 16)) (if (< (card-value dealers-card) 7) current-hand (hit (append (deck 'draw 1) current-hand)))) (else (if (memq (card-value dealers-card) '(4 5 6)) current-hand (hit (append (deck 'draw 1) current-hand)))))) (hit (list)))
Playing a Game (1) (define (make-blackjack-table player deck-count house-cut) (let ((deck (make-decks deck-count)) (bet 10) (takings 0)) (define (bust? hand) (> (score hand) 21)) (define (blackjack? hand) (and (has-ace? hand) (has-face-card? hand))) (define (play-round) (let ((dealers-hand (blackjack-dealer deck))) (let ((players-hand (player deck (showing-card dealers-hand)))) (cond ((bust? players-hand) -1) ((bust? dealers-hand) (- 1 house-cut)) ((blackjack? players-hand) 1.5) ((> (score players-hand) (score dealers-hand)) (- 1 house-cut)) (else -1)))))
Playing a Game (2) (lambda (message) (cond ((eq? message 'play) (let ((result (play-round))) (set! takings (+ takings (* result bet))))) ((eq? message 'takings) takings)))))