30
Types of the past exam Types of the past exam questions questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

  • View
    216

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

Types of the past exam Types of the past exam questionsquestionsI. Write/Complete Code

II. Evaluate Expressions

III. Analyze Complexity

IV. Meta-circular Evaluator

Page 2: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

I.I. Write Code: Tools of the Write Code: Tools of the tradetrade

Facts about Scheme◦Functions as data (e.g. pass as parameter)◦Special forms (e.g. lambda, define, if, and) ◦Data Types (e.g. symbol, pair, list, streams)◦Reference and Comparison (e.g. eq?, equal?) ◦Helpful functions (map, filter etc.)

Methods for building functions◦ Iteration/Recursion (nested helper function)◦Abstraction barriers (constructor/selector…)◦Bottom-up/Top-down

Page 3: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

I.I. Evaluate Expression: Tools of Evaluate Expression: Tools of the Tradethe Trade

Substitution-model◦ Substitute expressions for names

Environment◦Connection between names and

expressions◦Define rule◦Set rule

Page 4: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

Application Rule (for P)1. Create a new frame A2. Make A into an environment E

3. In A, bind parameters of P to the argument values

4. Evaluate the body of P - E as the current environment

Page 5: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

III.III. Analyze Complexity: Tools of Analyze Complexity: Tools of the tradethe trade

Choose input parameter(s) What operations will count?Write recurrence

◦T(a,b)=c*T(a/2n,b/2)+d*F(a/2,b/2)Method A: Guess solution and

prove by induction Method B: Open few stages , find

a rule, and calculate sum

Page 6: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

IV.IV. Meta- Circular Evaluator: Tools of Meta- Circular Evaluator: Tools of the Tradethe Trade

Apply-Eval loopEval: evaluate expression in a

given environmentApply : apply procedure to the

argumentsAdding new constructs:

◦Add case in “eval” to recognize new construct

◦Translate to other or evaluate directly

Page 7: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

StreamsStreamsProcess infinite stream of lists to

produce infinite stream of pairs◦Input: [ (0 1 2 3 0 5) (0 2) (1 0 2)..]◦Output: [(0.1) (0.2) (0.3) (0.5) (1.1)

(2.0) (2.2)…]

Each pair is a list number (starting from zero) and a position of non-zero element within the list

Assume each list has at least one non-zero element

Page 8: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

Streams: Solution Streams: Solution StrategyStrategyTop-bottom: decompose into

smaller problems then solve them

(define (process-stream strm num-lst) (stream-append

(process-list (stream-car strm) num-lst 0)

(process-stream (stream-cdr strm) (+ num-lst 1))

))2 sub-problems: process-list and stream-append

Page 9: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

Streams: Process-listStreams: Process-list(define (process-list lst lst-pos elem-pos)

(cond

((null? lst) null)

((= (car lst) 0) (process-list (cdr lst) lst-pos (+ elem-pos 1)))

(else

(stream-cons

(cons lst-pos elem-pos)

(process-list (cdr lst) lst-pos (+ elem-pos 1))))

))

Page 10: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

Streams: Bottom-UpStreams: Bottom-Up Easy to preprocess into stream

of triples◦(List-num pos value)

Step II: Filter-out zeros (stream-filter)

Page 11: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

Streams: Bottom-UpStreams: Bottom-Up(define (stream->triples strm list-num pos-num) (let ((first-lst (stream-car strm))) (if ((null? first-lst) (stream->triples (stream-cdr strm)(+ list-

num 1) 0)) (stream-cons (list list-num pos-num (car first)) (stream->triples (stream-cons (cdr first) (stream-cdr stream) ) list-num (+ pos-num 1) ) ) )))

Page 12: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

ListsA tree defined as (2 ((3 7) 8) ((4

5)))A: Reverse the order of the (direct)

sub-trees iteratively (((4 5)) ((3 7) 8) 2)

B: Reverse the order of the sub-trees in the levels matching a given predicate.

Page 13: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

Lists: AIterative – pass the result as an

argument.

(define (reverse-sub-trees tr)

(define (reverse-iter tr new-tr)

(if (null? tr)

new-tr

(reverse-iter (cdr tr) (cons (car tr) new-tr))))

(reverse-iter tr '()))

Page 14: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

Lists: B Need to process first sub-tree (car tr) and the other sub-

trees (cdr tr), and append the resulting sub-trees – order depends om (pred level).

(define (reverse-some-levels tr pred) (define (reverse-iter tr pred level) (cond ((null? tr) '()) ((not (pair? tr)) tr) ((pred level) (append (reverse-iter (cdr tr) pred level) (list (reverse-iter (car tr) pred (+ 1 level))))) (else (cons (reverse-iter (car tr) pred (+ 1 level)) (reverse-iter (cdr tr) pred level))))) (reverse-iter tr pred 1))

Page 15: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

SOS-interleave - Graphically

S11 S12 S13 . . .

S21 S22 S23 . . .

S31 S32 S33 . . .

. . .

1 2

3

Page 16: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

SOS-interleave - Code

(define (interleave s1 s2) (if (stream-null? s1) s2 (cons-stream (stream-car s1) (interleave s2 (stream-cdr s1)))))

(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 17: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

SOS-interleave - TestCreate an integers SOS:(define (int_strm int) (cons-stream int (int_strm int)))(define (ints n) (cons-stream (int_strm n) (ints (+ n

1))))> (define sos_ints (ints 1))sos_ints: [(1 1 1 1 1 …) (2 2 2 2 2 …) (3 3 3 3 3 …)

… ]

> (display-stream-head (sos-interleave sos_ints) 10) 1 1 2 1 2 1 3 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5

1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1

Page 18: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

Environments Model(define (make-sqrt x)

(define (improve guess)(average guess (/ x guess)))

(let ((guess 1.0))(lambda()

(set! guess (improve guess))guess)))

Assume that average is primitive

Page 19: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

Environments Model(define 3-root-1 (make-sqrt 3))(define 3-root-2 (make-sqrt (+ 2 1)))> (3-root-1) ___ _______________> (3-root-2) __________________> (3-root-1) __________________> guessreference to undefined identifier: guess

Page 20: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

Environments Model> (3-root-1) 2> (3-root-2) 2> (3-root-1)1.75> guesserror

Page 21: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

Environments Model

GE

make-sqrt: 3-root-1: 3-root-2:

P: x

B: (define (improve …

E1x: 3

Improve:

P: guess

B: (average…

Page 22: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

MCENew object:(object <object-name >

(<field1> <exp1>)

(<field2> <exp2>)

…(<fieldn> <expn>))

Page 23: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

MCEExample:(object point

(v1 0)

(v2 0)

(v3 0)

(set-point!

(lambda(x y z) (set! v1 x)(set! v2 y)(set! v3 z) ‘ok))

(mirror! (lambda() (set! v1 (- v1))

(set! v2 (- v2))

(set! v3 (- v3))

‘ok))

Page 24: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

MCE>(point v1)0>((point set-point!) 4 3 3)ok>(point v1)4>((point mirror))ok>(point v1)-4

Page 25: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

MCEPredicate: object-def?Selectors: object-name, obkect-

bindings’ binding-field, binding-exp

Eval-object-def – creates a new environment that includes all fields.

In the original env - object-name refers to:

(define (make-object env) (list ‘object env)

Page 26: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

MCEuse for-each – receives a proc

and a list and applies proc an each of the elements in the list (no returned value)

Page 27: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

(define (eval-object-def exp env)(let ((object-env

(extend-environment null null ________________)))

(for-each(lambda(binding)

(define-variable!______________________________________________________________________________________________________))

(object-bindings exp))(define-variable! __________________________________________________________________________________________________)‘ok))

Page 28: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

(define (eval-object-def exp env)(let ((object-env

(extend-environment null null ____env_________)))

(for-each(lambda(binding)

(define-variable!(binding-field binding)(mc-eval (binding-exp binding) env)object-env))

(object-bindings exp))(define-variable! (object-name exp)

(make-object object-env) env)‘ok))

Page 29: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator

MCE – cont’Read a value of a field:(object-name field-name)

(define (eval-object-access exp obj)(lookup-variable-value (cadr exp)(object-env object)))

Page 30: Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator