1 / 19

( ( lambda (z) ( define x ( lambda (x) ( lambda (y z) (y x) ) ) )

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

Download Presentation

( ( lambda (z) ( define x ( lambda (x) ( lambda (y z) (y x) ) ) )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

  5. 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

  6. Example Sorting: 0.31 0.44 0.72 0.89 0.10 0.05 0.97 0.23 0.56 0.68 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 0.31 0.10 0.05 0.23 0.72 0.89 0.97 0.68 0.44 0.56

  7. 0.05 0.10 0.23 0.31 0.68 0.72 0.89 0.97 0.44 0.56 Example Sorting: 0.31 0.44 0.72 0.89 0.10 0.05 0.97 0.23 0.56 0.68 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

  8. 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

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

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

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

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

  13. 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

  14. 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

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

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

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

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

More Related