CS 3015 Lecture 5 Higher Order Procedures - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

CS 3015 Lecture 5 Higher Order Procedures

Description:

Compute the sum of the cubes of the integers in a certain range ... (define (sum-cubes a b) (sum cube a inc b)) Defining Sum of Integers (define (identity x) x) ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 23
Provided by: jeffreyv
Category:

less

Transcript and Presenter's Notes

Title: CS 3015 Lecture 5 Higher Order Procedures


1
CS 3015 Lecture 5Higher Order Procedures
2
Number of Nodes in a Binary Tree
  • Basis nodes(ltgt) 0
  • Induction nodes(tree(L,a,R))
  • 1 nodes(L) nodes(R)

3
PreOrder Traversal
  • pre BinTreesA -gt ListsA
  • Basis pre(ltgt) ltgt
  • Induction pre(tree(L,a,R))
  • (list a (pre L) (pre R))
  • pre(tree(ltgt,A,tree(tree(ltgt,B,ltgt),C,ltgt)))
  • (list A () (list C (list B () ()) ()))

4
Higher-Order Procedures
  • Procedures give us a method of creating
    abstractions
  • Could get along without procedure cube by always
    writing expressions like ( 5 5 5)
  • But this would place us at a serious disadvantage
  • Powerful programming languages must always
    provide methods of abstraction
  • Procedures are one way to provide this

5
Not Enough
  • However, consider functions to do the following
  • Compute the sum of the integers a through b
  • Compute the sum of the cubes of the integers in a
    certain range
  • Compute the sum of a sequence of terms in the the
    series

6
Look at the Scheme Functions
  • (define (sum-integers a b)  (if (gt a b)      0 
         ( a (sum-integers ( a 1) b))))
  • (define (sum-cubes a b)  (if (gt a b)      0    
      ( (cube a) (sum-cubes 
  • ( a 1) b))))

7
  • (define (pi-sum a b)  (if (gt a b)      0      (
     (/ 1.0 ( a ( a 2)))
  •  (pi-sum ( a 4) b))))
  • Converges on 1/8 Pi.

8
Notice the Pattern
  • (define (ltnamegt a b)  (if (gt a b)      0      (
     (lttermgt a)         (ltnamegt (ltnextgt a) b))))
  • Wed like to be able to write a program that
    expresses the idea of summing a series without
    committing to computing particular sums

9
Procedures as Arguments
  • (define (sum term a next b)  (if (gt a b)      0
          ( (term a)         (sum term (next a) next
     b))))
  • (define (inc n) ( n 1))
  • (define (sum-cubes a b)  (sum cube a inc b))

10
Defining Sum of Integers
  • (define (identity x) x)
  • (define (sum-integers a b)  (sum identity a inc b
    ))

11
Defining Pi-Sum
  • (define (pi-sum a b)  (define (pi-term x)    (/ 
    1.0 ( x ( x 2))))  (define (pi-next x)    ( x
     4))  (sum pi-term a pi-next b))
  • ( 8 (pi-sum 1 1000))3.139592655589783

12
Defining Further Abstractions
  • Once we have sum we can define further
    abstractions
  • For example, the definite integral of a function
    f between limits a and b

13
Scheme Function to Compute Definite Integral
  • (define (integral f a b dx)  (define (add-dx x) (
     x dx))  ( (sum f ( a (/ dx 2.0)) add-dx b)  
       dx))
  • (integral cube 0 1 0.01)

14
Exercise
  • Write a procedure min-fx-gx that takes two
    numerical procedures f and g and a number x as
    inputs, and returns the minimum of applying f to
    x and g to x.

15
Exercise
  • Write a procedure min-fx-gx that takes two
    numerical procedures f and g and a number x as
    inputs, and returns the minimum of applying f to
    x and g to x.
  • (define (min-fx-gx f g x)
  • (min (f x) (g x))
  • (min-fx-gx square cube -1)
  • -1
  • (min-fx-gx square cube 2)
  • 4

16
Exercise contd
  • Now generalize your procedure to make the
    function applied to f(x) and g(x) be an input
    parameter also.
  • (define (combine-fx-gx h f g x)
  • (h (f x) (g x))
  • (combine-fx-gx min square cube -1)
  • -1

17
Constructing Procedures using lambda
  • Sometimes its awkward to have to define trivial
    procedures just to pass them as arguments
  • pi-next, pi-term
  • We introduce a new special form called lambda
  • (lambda (x) ( x 4))
  • (lambda (x) (/ 1.0 ( x ( x 2))))

18
New Definition of Pi-Sum
  • (define (pi-sum a b)  (sum (lambda (x) 
  • (/ 1.0 ( x ( x 2))))       a       (lambda
     (x) ( x 4))       b))

19
New Definition of Integral
  • (define (integral f a b dx)  ( (sum f          
    ( a (/ dx 2.0))          (lambda (x) ( x dx)) 
             b)     dx))

20
Lambda in General
  • General form
  • (lambda (ltformal-paramsgt) ltbodygt)
  • Read
  • (lambda  (x)  (    x     4)
  •  the procedure of an argument x  that adds  x and 
    4
  • (define (plus4 x) ( x 4))
  • is really just shorthand for
  • (define plus4 (lambda (x) ( x 4)))

21
So Now
  • ((lambda (x y z) ( x y (square z))) 
  • 1 2 3)

22
More Illustration of lambda
  • We can use integrals to get another method of
    approximating p, since p/4 arctan 1
  • (define (approx-pi dx)
  • ( 4 (integral
  • (lambda (x)
  • (/ 1 ( 1 ( x x))))
  • 0 1 dx)))
Write a Comment
User Comments (0)
About PowerShow.com