abstraction n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Abstraction PowerPoint Presentation
Download Presentation
Abstraction

Loading in 2 Seconds...

play fullscreen
1 / 12

Abstraction - PowerPoint PPT Presentation


  • 49 Views
  • Uploaded on

Abstraction. Lab 3 Karl Lieberherr. Abstractions in Scheme for traversing and processing data. lists map: same function applied to all elements records explicitly enumerate relevant elements Example: BusRoute example (Lab 1) (define (proc-towns towns) (map proc-town towns))

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Abstraction' - orea


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
abstraction

Abstraction

Lab 3

Karl Lieberherr

abstractions in scheme for traversing and processing data
Abstractions in Scheme for traversing and processing data
  • lists
    • map: same function applied to all elements
  • records
    • explicitly enumerate relevant elements
  • Example: BusRoute example (Lab 1)
    • (define (proc-towns towns)

(map proc-town towns))

    • (define (proc-town town)

(proc-busStops(Town-busStops town)))

abstractions in scheme for traversing and processing data1
Abstractions in Scheme for traversing and processing data

unions: explicitly enumerate relevant alternatives in cond clause.

;; capacity checking example

(define (weight-item l)

(cond

[(Simple? l) (Simple-weight l)]

[(Container? l) (weight-container l)]))

compacting information variants of map
compacting information: variants of map
  • (andmap p alox)
    • determine whether p holds for every item on alox
  • (ormap p alox)
    • determine whether p holds for at least one item on alox
compacting information with fold
Compacting information with fold
  • (foldr + 0 (map weight-item l)) (Capacity checking example)
  • Two versions: foldr and foldl
  • Very flexible because compacting function is a parameter (+ above)
container capacity example
Container capacity example

(define-struct Container (contents capacity))

(define-struct Simple (name weight))

a more interesting use of map and fold
A more interesting use of map and fold

(define (check ac)   (wv-violations    ((make-Container-Visitor      (lambda (name weight) (make-wv weight 0))      (lambda (wvs capacity)        (let* ((totals                (foldr (lambda (wv1 wv2)                         (make-wv                          (+ (wv-weight wv1) (wv-weight wv2))                          (+ (wv-violations wv1) (wv-violations wv2))))                       (make-wv 0 0)                       wvs))               (total-weight (wv-weight totals))               (total-violations (wv-violations totals)))          (make-wv total-weight                   (if (> total-weight capacity)                       (+ 1 total-violations)                       total-violations)))))     ac)))

functional visitor does traversal
Functional Visitor: does traversal

(define (make-Container-Visitor simple-visitor container-visitor)

(define (visitor c)

(cond ((Simple? c)

(simple-visitor (Simple-name c) (Simple-weight c)))

((Container? c)

(container-visitor (map visitor (Container-contents c))

(Container-capacity c)))

(else ???)))

visitor)

filter
filter

;; filter : (X -> boolean) (listof X) -> (listof X)

(eliminate-expensive u toys)

Question: does it make sense to generalize filter to any kind of Scheme structure?

  • consider a container c
  • filter-struct : (X -> boolean) (structure containing X) -> (structure containing X)
exercise filter
Exercise: filter
  • Design a dir structure (HTDP 224 -226) and filter out all files whose size is greater than 100.
  • Choose a structure that minimizes conditionals.
apply from help desk
apply: from help desk

procedure:  (apply proc arg1 ... args) 

proc must be a procedure and args must be a list. Calls proc with the elements of the list (append (list arg1 ...) args) as the actual arguments.

(apply + (list 3 4))                      ===>  7

(define compose

  (lambda (f g)

    (lambda args

      (f (apply g args)))))

((compose sqrt *) 12 75)                      ===>  30

exercise apply and eliminate lambda
Exercise: apply and eliminate lambda

(define compose

(lambda (f g)

(lambda args

(f (apply g args)))))

(define (compose2 f g)

(lambda args

(f (apply g args))))

(define (compose3 f g)

(local (define (myf args) (f (apply g args))))

myf))

((compose sqrt *) 12 75)

((compose2 sqrt *) 12 75)

((compose3 sqrt *) '(12 75))

How can we write compose3

without requiring the ‘?