Functional Abstraction - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Functional Abstraction

Description:

Another way to think of abstraction. A black box ... This abstraction is relies on term and next being procedures / functions. ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 27
Provided by: ben1
Category:

less

Transcript and Presenter's Notes

Title: Functional Abstraction


1
  • Functional Abstraction

2
What Happened Yesterday
  • Data abstraction / ????
  • Rational numbers
  • A function to create them
  • Functions to manipulate them
  • Ways of combining and accessing data
  • cons, list, car, cdr, list-ref

3
What Will Happen Today
  • Review of abstraction as a useful tool
  • Learning about functional abstraction
  • Another problem with languages like C, C, and
    Java!
  • A way to create local variables

4
What Is 'Data'?
  • Defined by
  • Selectors (ways to access)
  • Constructors (ways to make)
  • Rules that ensure a good representation
  • Example
  • Xiao Xiao's rational number
  • Lists (from yesterday)

5
Some More Ideas
  • Abstract properties
  • What the data is supposed to do / how it behaves
  • Example Lists using cons, car, cdr
  • Implementation
  • The exact/concrete way that something is built
  • Think about the many ways to define exponentiation

6
Abstraction /???? Defined
  • Separating the abstract properties
  • of data from its implementation

7
Another way to think of abstraction
  • A black box
  • You know what the inputs and outputs are / how
    they behave
  • The implementation is completely hidden
  • Assume that the black box works correctly

8
Black Box Examples
9
Why is abstraction useful?
  • Allows for modularity / You can change things
  • Makes things easier to generalize
  • Things are easier to scale to a large size
  • A useful way to control complexity

10
Real World Abstractions
  • TV remote
  • AC adapter
  • Sigma notation
  • ? f(x) f(0)...f(n)
  • Circuit elements
  • resistor, capacitor, current source, etc.
  • Thermostat
  • Piano keys

11
How To Build An Abstraction
  • Assign names to common patterns
  • Don't use ( x x x)
  • Do use (cube x)
  • Think about what you should be given and what you
    should give
  • Remember that only you should need to know how
    things actually work

12
Higher Order Procedures
  • Procedures that manipulate or change other
    procedures
  • Good way of creating abstractions
  • Not truly supported in many languages!

13
Summing Numbers
  • The sum of integers from a to b(define
    (sum-integers a b) (if (gt a b) 0 (
    a (sum-integers ( a 1) b))))
  • The sum of the cubes of integers from a to
    b(define (sum-cubes a b) (if (gt a b) 0
    ( (cube a) (sum-cubes ( a 1)
    b))))

14
Summing Numbers
  • Thanks to Leibniz we know that ?/8 (define
    (pi-sum a b) (if (gt a b) 0 ( (/
    1.0 ( a ( a 2))) (pi-sum ( a 4) b))))

15
Hints for creating abstraction
  • Look for patterns then make an abstraction
  • Do you see a pattern?
  • (define (pi-sum a b) (if (gt a b) 0
    ( (/ 1.0 ( a ( a 2))) (pi-sum ( a 4)
    b))))
  • (define (sum-int a b) (if (gt a b) 0
    ( a (sum-int (inc a) b))))
  • (define (sum-cubes a b) (if (gt a b) 0
    ( (cube a) (sum-cubes ( a 1) b))))

16
Template for Summing
  • Identifying the pattern(define (ltnamegt a b)
    (if (gt a b) 0 ( (lttermgt a)
    (ltnamegt (ltnextgt a) b))))
  • Implementing the pattern(define (sum term a
    next b) (if (gt a b) 0 ( (term a)
    (sum term (next a) next b))))

17
Summing Using Our Abstraction
  • Summing integers(define (identity a) a)(define
    (inc a) ( a 1))(define (sum-integers a b)
    (sum identity a inc b))
  • Summing cubes of integers(define (sum-integers
    a b) (sum cube a inc b))

18
Summing Using Our Abstraction
  • ? (define (term a) (/ 1.0 ( a ( a
    2))))(define (sum-pi a b) ( 8 (sum
    term a
    (lambda (a) ( a 4)) b)))

19
Remember!
  • (define (sum term a next b) (if (gt a b)
    0 ( (term a) (sum term (next a)
    next b))))
  • This abstraction is relies on term and next being
    procedures / functions.
  • Our language must support passing functions
    (Scheme does!)

20
Defining Local Variables
  • Want to create a function with less fuss
  • Defining all of those helper procedures
    (identity, inc, dec, etc.) is messy
  • Is there a better way to do it?
  • Let us work with a multi-variable functionf(x,
    y) x2(1xy) y(1-x) 3

21
Old Way
  • If a (1xy) and b (1-x) thenf(x, y) ax2
    by 3
  • (define (a x y) ( 1 ( x y)))(define (b x y) (-
    1 x))(define (f x y) ( ( (square x) (a x
    y)) ( y (b x y)) 3))
  • Bad! This requires that things be defined
    outside the function when they are only used
    inside the function

22
Newer and Better Way
  • You can have a define within a define to use
    later in your code(define (f x y) (define
    (f-helper a b) ( ( (square x) a) ( y b)
    3)) (f-helper ( 1 ( x y)) (- 1 x)))

23
Newer and Slightly Better Way
  • Instead of having an extra define, use an unnamed
    lambda and pass it arguments(define (f x y)
    ((lambda (a b) ( ( (square x) a) ( y b)
    3)) ( 1 ( x y)) (- 1 x)))

24
Special form let
  • New way of doing things binds 'a' and 'b' to the
    specific values(define (g x y) (let ((a ( 1
    ( x y))) (b (- 1 x))) ( ( (square
    x) a) ( y b) 3)))

25
  • The general form of a let expression is(let
    ((ltvar1gt ltexp1gt) (ltvar2gt ltexp2gt)
    (ltvarngt ltexpngt))ltbodygt)
  • Equal tolet ltvar1gt have the value ltexp1gt and
    ltvar2gt have the value ltexp2gt and ltvarngt have
    the value ltexpngtin ltbodygt

26
What We Covered
  • Abstraction
  • Procedural Abstraction
  • The special form let
Write a Comment
User Comments (0)
About PowerShow.com