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

Click here to load reader

  • date post

    03-Jan-2016
  • Category

    Documents

  • view

    46
  • download

    5

Embed Size (px)

description

( ( 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 - PowerPoint PPT Presentation

Transcript of ( ( 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)

  • ExpressionEnvComment1((lambda1(z) op1 op2) 2)GE(op1 op2)2(op1 op2)E13Op1(define x (lambda2 (x) (lambda3(y z) (y x))))E14Op2(((x (lambda5 () z)) (lambda4(z) z) 3))((op3 (lambda4 (z) z) 3))E15op3(x (lambda5 () z)) E1l36((l3 l4 3))E17(y x)E3(l4 l5)8ZE4l59(l5)E110ZE52

  • GE

  • *VectorsConstructors:(vector v1 v2 v3 . . .)(make-vector size init)

    Selector:(vector-ref vec place)

    Mutator:(vector-set! vec place value)

    Other functions:(vector-length vec)

  • *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)))

  • *Bucket SortProblem: 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-intervals sizeIdea: Divide into sub-intervals (buckets)Throw each number into appropriate bucketSort within each bucket (any sorting method)Adjoin all sorted buckets

  • *ExampleIf 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.23Bucket 0.333 0.667: 0.44 0.56Bucket 0.667 1 : 0.72 0.89 0.97 0.68Sorting:0.31 0.44 0.72 0.89 0.10 0.05 0.97 0.23 0.56 0.680.31 0.10 0.05 0.23

    0.44 0.560.72 0.89 0.97 0.68

  • *ExampleSorting:0.31 0.44 0.72 0.89 0.10 0.05 0.97 0.23 0.56 0.68Now sort!Bucket 0.000 - 0.333: 0.05 0.10 0.23 0.31 Bucket 0.333 0.667: 0.44 0.56Bucket 0.667 1 : 0.68 0.72 0.89 0.97

  • *ExampleSorting:0.31 0.44 0.72 0.89 0.10 0.05 0.97 0.23 0.56 0.68Adjoin sorted buckets:0.05 0.10 0.23 0.31 0.44 0.56 0.68 0.72 0.89 0.97

  • *AnalysisObservation: 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)

  • *ImplementationA bucket is a listThe set of buckets is a vectorThe 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))))))

  • *Implementation for-eachScheme primitiveSimilar to map, without returning a valueUseful for side-effects(define (for-each proc lst) (if (null? lst) done (begin (proc (car lst)) (for-each proc (cdr lst)))))

  • *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)))

  • *Polish NotationPostFix Notation: operands before operatorsExpressions with binary operators can be written without ParenthesesApply operators, from left to right, on the last two numbers5 7 4 - 2 * +5 3 2 * +5 6 + 11Stack implementation:Init: Empty stackNumber: insert! Into stackOperator: apply on 2 top stack elements and insert! result into stack

  • *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

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

  • *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)))))

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

  • *Simulation(calc)574-TOP= 32*TOP= 6+TOP= 11exitok

    ***