Abstraction - PowerPoint PPT Presentation

About This Presentation
Title:

Abstraction

Description:

Lab 3. Karl Lieberherr. Abstractions in Scheme for traversing and processing data. lists ... Abstractions in Scheme for traversing and processing data ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 13
Provided by: karljlie
Category:

less

Transcript and Presenter's Notes

Title: Abstraction


1
Abstraction
  • Lab 3
  • Karl Lieberherr

2
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)))

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

4
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

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

6
Container capacity example
  • (define-struct Container (contents capacity))
  • (define-struct Simple (name weight))

7
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
    (gt total-weight capacity)                     
     ( 1 total-violations)                     
     total-violations)))))     ac)))

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

9
filter
  • filter (X -gt boolean) (listof X) -gt (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 -gt boolean) (structure
    containing X) -gt (structure containing X)

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

11
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))                      gt  7
  • (define compose
  •   (lambda (f g)
  •     (lambda args
  •       (f (apply g args)))))
  • ((compose sqrt ) 12 75)                      gt
      30

12
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 ?
Write a Comment
User Comments (0)
About PowerShow.com