Presentation is loading. Please wait.

Presentation is loading. Please wait.

( (lambda (z) (define x (lambda (x) (lambda (y z) (y x)))) ( ( (x (lambda () z)) (lambda (z) z) 3 ) ) ) 2)

Similar presentations


Presentation on theme: "( (lambda (z) (define x (lambda (x) (lambda (y z) (y x)))) ( ( (x (lambda () z)) (lambda (z) z) 3 ) ) ) 2)"— Presentation transcript:

1 ( (lambda (z) (define x (lambda (x) (lambda (y z) (y x)))) ( ( (x (lambda () z)) (lambda (z) z) 3 ) ) ) 2)

2 ExpressionEnvComment 1((lambda1(z) op1 op2) 2)GE(op1 op2) 2 E1 3Op1 (define x (lambda2 (x) (lambda3(y z) (y x)))) E1 4Op2 (((x (lambda5 () z)) (lambda4(z) z) 3)) ((op3 (lambda4 (z) z) 3)) E1 5op3 (x (lambda5 () z)) E1l3 6((l3 l4 3))E1 7(y x)E3(l4 l5) 8ZE4l5 9(l5)E1 10ZE52

3 L1:p: z b: (define x...) (...) E1 z: 2 x:L2 L2:p: x b: (lambda (y z) (y x))) L5:p: - b: z E2 x:L5 L3:p: y,z b: (y x) L4:p: z b: z E3 y: L4 z: 3 E4 z: L5 E5 GE

4 4 Vectors Constructors: (vector v1 v2 v3...) (make-vector size init) Selector: (vector-ref vec place) Mutator: (vector-set! vec place value) Other functions: (vector-length vec)

5 5 Example - accumulating (define (accumulate-vec op base vec) (define (helper from to) (if (> from to) base (op (vector-ref vec from) (helper (+ from 1) to)))) (helper 0 (- (vector-length vec) 1)))

6 6 Bucket Sort Problem: Sorting numbers that distribute “uniformly” across a given interval (for example, [0,1) ). Observation: The number of elements that fall within a sub- interval is proportional to the sub-interval’s size Idea: – Divide into sub-intervals (buckets) – Throw each number into appropriate bucket – Sort within each bucket (any sorting method) – Adjoin all sorted buckets

7 7 Example If we want ~3 numbers in each bucket, we need 3-4 buckets. Using 3 buckets we have: Bucket 0.000 - 0.333: 0.31 0.10 0.05 0.23 Bucket 0.333 – 0.667: 0.44 0.56 Bucket 0.667 – 1 : 0.72 0.89 0.97 0.68 Sorting: 0.31 0.44 0.72 0.89 0.10 0.05 0.97 0.23 0.56 0.68 0.31 0.10 0.05 0.23 0.44 0.56 0.72 0.89 0.97 0.68

8 8 Example Sorting: 0.31 0.44 0.72 0.89 0.10 0.05 0.97 0.23 0.56 0.68 0.05 0.10 0.23 0.31 0.44 0.56 0.68 0.72 0.89 0.97 Now sort! Bucket 0.000 - 0.333: 0.05 0.10 0.23 0.31 Bucket 0.333 – 0.667: 0.44 0.56 Bucket 0.667 – 1 : 0.68 0.72 0.89 0.97

9 9 Example Sorting: 0.31 0.44 0.72 0.89 0.10 0.05 0.97 0.23 0.56 0.68 Adjoin sorted buckets: 0.05 0.10 0.23 0.31 0.44 0.56 0.68 0.72 0.89 0.97

10 10 Analysis Observation: If number of buckets is proportional to the number of elements, then the number of elements in each bucket is more or less the same, and bounded by a constant. Dividing into buckets: O(n) Sorting one bucket: O(1) Sorting all buckets: O(n) Adjoining sorted sequences: O(n) All together : O(n)

11 11 Implementation A bucket is a list The set of buckets is a vector The elements are sorted while inserted into the buckets (very similar so insertion sort): (define (insert x s) (cond ((null? s) (list x)) ((< x (car s)) (cons x s)) (else (cons (car s) (insert x (cdr s))))))

12 12 Implementation – for-each Scheme primitive Similar to map, without returning a value Useful for side-effects (define (for-each proc lst) (if (null? lst) ‘done (begin (proc (car lst)) (for-each proc (cdr lst)))))

13 13 Implementation – cont. (define (bucket-sort s) (let* ((size 5) (n (ceiling (/ (length s) size))) (buckets (make-vector n null))) (define (insert! x) (let ((bucket (inexact->exact (floor (* n x))))) (vector-set! buckets bucket (insert x (vector-ref buckets bucket))))) (for-each insert! s) (accumulate-vec append null buckets)))

14 14 Polish Notation PostFix Notation: operands before operators Expressions with binary operators can be written without Parentheses Apply operators, from left to right, on the last two numbers – 5 7 4 - 2 * + – 5 3 2 * + – 5 6 + – 11 Stack implementation: – Init: Empty stack – Number: insert! Into stack – Operator: apply on 2 top stack elements and insert! result into stack

15 15 Read & Eval (read) - returns an expression from the user (eval exp) - evaluates an expression We will use these functions in: – (perform m) - receives a symbol of an operation, applies it on the two top numbers in the stack, and returns the result to the stack – (iter) - reads an input from the user. If it is a number it is pushed to the stack, if it is an operator we call perform

16 16 perform (define (perform m) (let ((arg2 ((stk 'top)))) ((stk 'delete!)) (let ((arg1 ((stk 'top)))) ((stk 'delete!)) ((stk 'insert!) ((eval m) arg1 arg2)))))

17 17 iter (define (iter) (let ((in (read))) (if (eq? in 'exit) 'ok (begin (cond ((number? in) ((stk 'insert!) in)) (else (perform in) (display "TOP= ") (display ((stk 'top))) (newline))) (iter)))))

18 18 Calc (define (calc) (let ((stk (make-stack))) (define (perform m)...) (define (iter)... ) (iter)))

19 19 Simulation (calc) 5 7 4 - TOP= 3 2 * TOP= 6 + TOP= 11 exit ok


Download ppt "( (lambda (z) (define x (lambda (x) (lambda (y z) (y x)))) ( ( (x (lambda () z)) (lambda (z) z) 3 ) ) ) 2)"

Similar presentations


Ads by Google