Top Banner
CS1 Final Exam Review By Rebecca Schulman December 4, 2002
43

CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

Dec 17, 2015

Download

Documents

Ronald Copeland
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

CS1 Final Exam Review

By Rebecca Schulman

December 4, 2002

Page 2: 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

Page 3: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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

Page 4: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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

Page 5: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

The Substitution Model

Three steps:

• Evaluate the operands

• Evaluate the operator

• Apply the operator to the operands

Page 6: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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

Page 7: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

More Substitution and Mutation

• Remember also that begin statements must be 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

Page 8: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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

Page 9: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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

Page 10: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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

Page 11: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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)

Page 12: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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

Page 13: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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)))

Page 14: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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

Page 15: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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

Page 16: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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.

Page 17: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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

Page 18: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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)

Page 19: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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)

Page 20: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

Message Passing

• Message Passing allowed us to encapsulate data and operations into the same object

Page 21: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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

Page 22: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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

Page 23: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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

Page 24: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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

Page 25: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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

Page 26: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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 ((value initial)) (lambda (change) (set! value (+ value change)) value)))

Page 27: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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

Page 28: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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))

Page 29: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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…

Page 30: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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

Page 31: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

The Deck of Cards

• We begin with the ranks and suits

(define suits '(clubs diamonds hearts spades))

(define ranks '(A 2 3 4 5 6 7 8 9 10 J Q K))

The deck of cards are simply all combinations of these

Page 32: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

List processing: all-combinations(define (all-combinations proc first second) (define (helper a b current) (cond ((null? a) current)

((null? (cdr b)) (helper (cdr a) second (cons (proc (car a) (car b))

current))) (else (helper a (cdr b) (cons (proc (car a) (car b))

current))))) (helper first second (list)))

Page 33: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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)))))

Page 34: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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

Page 35: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

(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))))

Page 36: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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))

Page 37: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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)))))))

Page 38: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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)))

Page 39: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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)))

Page 40: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

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)))))

Page 41: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

Playing a Game (2)

(lambda (message)

(cond ((eq? message 'play)

(let ((result (play-round)))

(set! takings

(+ takings (* result bet)))))

((eq? message 'takings)

takings)))))

Page 42: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

The Game’s Environment: The beginning

Page 43: CS1 Final Exam Review By Rebecca Schulman December 4, 2002.

The Game in Play