extended introduction to cs preparation for final exam

26
Extended Introduction to CS Preparation for Final Exam

Post on 20-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Extended Introduction to CS Preparation for Final Exam

Extended Introduction to CS

Preparation for Final Exam

Page 2: Extended Introduction to CS Preparation for Final Exam

Mutation

Page 3: Extended Introduction to CS Preparation for Final Exam

rotate!

• (define l ‘(1 2 3 4))

• (rotate! l)

• l (4 1 2 3) • First attempt: use set-cdr! • Would it work?• In which environment is l? and lst?

1 2 3 4

l

lst

Page 4: Extended Introduction to CS Preparation for Final Exam

Correct solution

(define (rotate! lst) (define (help p rest-lst) (cond ((null? rest-lst) (set-car! lst p)) (else (let ((temp (car rest-lst))) (set-car! rest-lst p) (help temp (cdr rest-lst)))))) (help (car lst) (cdr lst)) lst)

Page 5: Extended Introduction to CS Preparation for Final Exam

High-order functions

• Functions can receive other functions as input and may return functions as output

Page 6: Extended Introduction to CS Preparation for Final Exam

make-star

g*(x) = number of times we need to apply g until g(g(g(…(g(x)…)))<=1

Page 7: Extended Introduction to CS Preparation for Final Exam

make-star

(define (make-star g)

(define (g* x)

(if (<= x 1)

0

(+ 1

(g* (g x)))))

g*

)

Page 8: Extended Introduction to CS Preparation for Final Exam

log

(log 4) ==> 2

(log 1) ==> 0

(log 5) ==> 3

(define log

(make-star _________________ )(lambda (x) (/ x 2))

Page 9: Extended Introduction to CS Preparation for Final Exam

Streams

Page 10: Extended Introduction to CS Preparation for Final Exam

SOS-interleave

• Receives a stream of streams

• Should return a stream of all members (in some order)

Page 11: Extended Introduction to CS Preparation for Final Exam

(define (sos-interleave sos)

(let ((first-of-first (stream-car (stream-car sos))) (rest-of-first (stream-cdr (stream-car sos)) (rest (stream-cdr sos))) (cons-stream ________________________

________________________)))

Page 12: Extended Introduction to CS Preparation for Final Exam

SOS-interleave

(define (sos-interleave sos)

(let ((first-of-first (stream-car (stream-car sos))) (rest-of-first (stream-cdr (stream-car sos)) (rest (stream-cdr sos))) (cons-stream first-of-first

(interleave rest-of-first

(sos-interleave rest)))))

Page 13: Extended Introduction to CS Preparation for Final Exam

Words

• List of symbols (‘a or ‘b) represent a word

• A stream of lists represents a language

• 1. Generate the language of all words containing only ‘a’

• 2. Generate the language of all words starting with an ‘a’

Page 14: Extended Introduction to CS Preparation for Final Exam

All-a

(define all-a

(cons-stream '(a)

(stream-map (lambda (x) (append x '(a)))

all-a))))

Page 15: Extended Introduction to CS Preparation for Final Exam

Starting-with-a

(define start-with-a (cons-stream '(a) (interleave (stream-map (lambda (x) (append x '(a))) start-with-a) (stream-map (lambda (x) (append x '(b))) start-with-a))))

Page 16: Extended Introduction to CS Preparation for Final Exam

MC-eval

Page 17: Extended Introduction to CS Preparation for Final Exam

MC-eval

• Add a new special form – decrease

• (decrease proc num)

• If proc is compound, change its body so that it will return a number smaller by num than what it should have

• If proc is not compound return error

• Otherwise assume it returns a number

Page 18: Extended Introduction to CS Preparation for Final Exam

;;; M-Eval input:(define (double x) (+ x x));;; M-Eval value:ok;;; M-Eval input:(double 3);;; M-Eval value:6;;; M-Eval input: (decrease double 1);;; M-Eval value:ok;;; M-Eval input:(double 3);;; M-Eval value:5;;; M-Eval input(decrease + 1)Error: can apply decrase only to compound procedures

Page 19: Extended Introduction to CS Preparation for Final Exam

Section A

(define (decrease? exp)

___________________________ )

(define (decrease-proc-name exp)

______________________ )

(define (decrease-number exp)

_______________________ )

Page 20: Extended Introduction to CS Preparation for Final Exam

(define (decrease? exp)

___(tagged-list? exp 'decrease)___________________ )

(define (decrease-proc-name exp)

____(cadr exp)__________________________ )

(define (decrease-number exp)

_____(caddr exp)_______________________ )

Page 21: Extended Introduction to CS Preparation for Final Exam

Section BDecrease returns the new procedure body

(define (modify-body proc expressions-list) (set-car! (cddr proc) expressions-list))

(define (eval-decrease exp env) (let* ((proc-name ________________________________ )

(number ________________________________ )

(proc (_________________________ proc-name env)))

(cond ( ____________________________________

(error “Can apply decrease only to compound procedures”))

(else (modify-body proc (decrease proc number)) ‘ok))))

Page 22: Extended Introduction to CS Preparation for Final Exam

(define (modify-body proc expressions-list) (set-car! (cddr proc) expressions-list))

(define (eval-decrease exp env) (let* ((proc-name ____(decrease-proc-name exp)________ )

(number ____(decrease-number exp)_____________ )

(proc (_____mc-eval _____________ proc-name env)))

(cond ( _____(not (compound-procedure? proc))____________

(error “Can apply decrease only to compound procedures”))

(else (modify-body proc (decrease proc number)) ‘ok))))

Page 23: Extended Introduction to CS Preparation for Final Exam

Environment Model

Page 24: Extended Introduction to CS Preparation for Final Exam

Functional tables

(define (make-table) (lambda (x) (error "key not found:" x)))

(define (find key table) (table key))

(define (insert key value table) (lambda (x) (if (equal? x key) value (find x table))))

Q2

Page 25: Extended Introduction to CS Preparation for Final Exam

Example

(define t0 (make-table))

(define t1 (insert 'a 1 t0))

(define t2 (insert 'b 2 t1))

(find 'a t2) => 1

Page 26: Extended Introduction to CS Preparation for Final Exam

globalenv

Pars: –Body: (lambda (x) (error ...))

make-table:

Pars: key, tableBody: (table key)

find:

Pars: key, table, valueBody: (lambda (x) (if ...))

insert:

E1:

Pars: xBody: (error ...))

t0:

Pars: xBody: (if ...))

key: avalue: 1table:

E2:

t1:

key: atable:

E4:

key: atable:

E6:

x: aE5:

x: aE7:

key: bvalue: 2table:

E3:

Pars: xBody: (if ...))

t2: