extended introduction to cs preparation for final exam
Post on 20-Dec-2015
218 Views
Preview:
TRANSCRIPT
Extended Introduction to CS
Preparation for Final Exam
Mutation
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
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)
High-order functions
• Functions can receive other functions as input and may return functions as output
make-star
g*(x) = number of times we need to apply g until g(g(g(…(g(x)…)))<=1
make-star
(define (make-star g)
(define (g* x)
(if (<= x 1)
0
(+ 1
(g* (g x)))))
g*
)
log
(log 4) ==> 2
(log 1) ==> 0
(log 5) ==> 3
(define log
(make-star _________________ )(lambda (x) (/ x 2))
Streams
SOS-interleave
• Receives a stream of streams
• Should return a stream of all members (in some order)
(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 ________________________
________________________)))
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)))))
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’
All-a
(define all-a
(cons-stream '(a)
(stream-map (lambda (x) (append x '(a)))
all-a))))
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))))
MC-eval
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
;;; 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
Section A
(define (decrease? exp)
___________________________ )
(define (decrease-proc-name exp)
______________________ )
(define (decrease-number exp)
_______________________ )
(define (decrease? exp)
___(tagged-list? exp 'decrease)___________________ )
(define (decrease-proc-name exp)
____(cadr exp)__________________________ )
(define (decrease-number exp)
_____(caddr exp)_______________________ )
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))))
(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))))
Environment Model
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
Example
(define t0 (make-table))
(define t1 (insert 'a 1 t0))
(define t2 (insert 'b 2 t1))
(find 'a t2) => 1
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:
top related