SlideShare a Scribd company logo
4.2 Composing procedures
One way to divide a problem is to split it into steps where the output of the first step is
the input to the second step, and the output of the second step is the solution to
the problem.
Each step can be defined by one procedure, and the two procedures can be combined to
create one procedure that solves the problem.
Figure 4.2 shows a composition of two functions, f and g. The output of f is used as the
input to g.
Figure 4.2: Composition.
We can express this composition with the Scheme expression (g (f x)) where xis the
input.
The written order appears to be reversed from the picture in Figure 4.2.
This is because we apply a procedure to the values of its subexpressions: the values of
the inner subexpressions must be computed first, and then used as the inputs to
the outer applications.
So, the inner subexpression(f x) is evaluated first since the evaluation rule for the outer
application expression is to first evaluate all the subexpressions.
To define a procedure that implements the composed procedure we make x a parameter:
(define fog (lambda (x) (g (f x))))
This defines fog as a procedure that takes one input and produces as output the
composition of f and g applied to the input parameter.
This works for any two procedures that both take a single input
parameter.
We can compose the square and cube procedures from Chapter 3:
(define sixth-power (lambda (x) (cube (square x))))
Then, (sixth-power 2) evaluates to 64.
4.2.1 Procedures as inputs and outputs
All the procedure inputs and outputs we have seen so far have been numbers.
The subexpressions of an application can be any expression including a procedure.
A higher-order procedure is a procedure that takes other procedures as inputs or that
produces a procedure as its output.
Higher-order procedures give us the ability to write procedures that behave differently
based on the procedures that are passed in as inputs.
We can create a generic composition procedure by making f and g parameters:
(define fog (lambda (f g x) (g (f x))))
The fog procedure takes three parameters. The first two are both procedures that take
one input.
The third parameter is a value that can be the input to the first procedure.
For example, (fog square cube 2) evaluates to 64, and (fog (lambda (x) (+ x 1))
square
2) evaluates to 9.
In the second example, the first parameter is the procedure produced by the
lambda expression (lambda (x) (+ x 1)).
This procedure takes a number as input and produces as output that number plus one.
We use a definition to name this procedure inc (short for increment):
(define inc (lambda (x) (+ x 1)))
A more useful composition procedure would separate the input value, x, from the
composition.
The fcompose procedure takes two procedures as inputs and produces as output a
procedure that is their composition:1
(define fcompose (lambda (f g) (lambda (x) (g (f x)))))
The body of the fcompose procedure is a lambda expression that makes a procedure.
Hence, the result of applying fcompose to two procedures is not a simple value, but
a procedure. The resulting procedure can then be applied to a value.
Here are some examples using fcompose:
Scheme output
> (fcompose inc inc)
# < procedure >
> ((fcompose inc inc) 1)
3
> ((fcompose inc square) 2)
9
> ((fcompose square inc) 2)
5
Exercise 4.1. For each expression, give the value to which the expression
evaluates. Assume fcompose and inc are defined as above.
•((fcompose square square) 3)
•(fcompose (lambda (x) (* x 2)) (lambda (x) (/ x 2)))
•((fcompose (lambda (x) (* x 2)) (lambda (x) (/ x 2))) 1120)
•((fcompose (fcompose inc inc) inc) 2)
Exercise 4.2. Suppose we define self-compose as a procedure that composes a
procedure with itself: (define (self-compose f) (fcompose f f)) Explain how(((fcompose self-
compose self-compose) inc) 1) is evaluated.
Exercise 4.3. Define a procedure fcompose3 that takes three procedures as input, and
produces as output a procedure that is the composition of the three input procedures.
For example, ((fcompose3 abs inc square) -5) should evaluate to36. Define fcompose3 two
different ways: once without using fcompose, and once using fcompose.
Exercise 4.4. The fcompose procedure only works when both input procedures
take one input. Define a f2compose procedure that composes two procedures where the
first procedure takes two inputs, and the second procedure takes one input.
For example, ((f2compose + abs) 3 -5) should evaluate to 2.
Exercise 4.1. For each expression, give the value to which the expression
evaluates. Assume fcompose and inc are defined as above.
•((fcompose square square) 3)
•(fcompose (lambda (x) (* x 2)) (lambda (x) (/ x 2)))
•((fcompose (lambda (x) (* x 2)) (lambda (x) (/ x 2))) 1120)
•((fcompose (fcompose inc inc) inc) 2)
Exercise 4.2. Suppose we define self-compose as a procedure that composes a
procedure with itself: (define (self-compose f) (fcompose f f)) Explain how(((fcompose self-
compose self-compose) inc) 1) is evaluated.
Exercise 4.3. Define a procedure fcompose3 that takes three procedures as input, and
produces as output a procedure that is the composition of the three input procedures.
For example, ((fcompose3 abs inc square) -5) should evaluate to36. Define fcompose3 two
different ways: once without using fcompose, and once using fcompose.
Exercise 4.4. The fcompose procedure only works when both input procedures
take one input. Define a f2compose procedure that composes two procedures where the
first procedure takes two inputs, and the second procedure takes one input.
For example, ((f2compose + abs) 3 -5) should evaluate to 2.

More Related Content

PPT
recursive problem_solving
PPT
evaluating recursive_applications
PPT
Type Casting in C++
PPTX
Function Pointer in C
PPTX
Function
PDF
Notes: Verilog Part 5 - Tasks and Functions
PPT
Type conversions
PPTX
Types of function call
recursive problem_solving
evaluating recursive_applications
Type Casting in C++
Function Pointer in C
Function
Notes: Verilog Part 5 - Tasks and Functions
Type conversions
Types of function call

What's hot (20)

PPT
Prsentation on functions
PPTX
parameter passing in c#
PPTX
Pointers lesson 5 (double pointer, call by value, call_by_reference)
PPTX
Method parameters in c#
PDF
Functions and tasks in verilog
PPT
RECURSION IN C
PPTX
Function Parameters
PPTX
functions
PDF
Domain and range (linear, quadratic, rational functions)
PPTX
PPT
First Look at Pointers
PPT
Recursion in c
PPTX
Highorderfunctions in swift
PPTX
Recursion for GCE AS Computing
PPT
17recursion
PPTX
Function in C program
PPTX
Function overloading
PPTX
Presentation on function
PPTX
Call by value
PPTX
Parameter passing to_functions_in_c
Prsentation on functions
parameter passing in c#
Pointers lesson 5 (double pointer, call by value, call_by_reference)
Method parameters in c#
Functions and tasks in verilog
RECURSION IN C
Function Parameters
functions
Domain and range (linear, quadratic, rational functions)
First Look at Pointers
Recursion in c
Highorderfunctions in swift
Recursion for GCE AS Computing
17recursion
Function in C program
Function overloading
Presentation on function
Call by value
Parameter passing to_functions_in_c
Ad

Viewers also liked (18)

PPT
universality
PPT
best first-sort
PPT
language construction
PPT
packaging procedures_and_state
PPT
Array1
PPT
evaluation rules
PPTX
Inference rulesproofmethods
PPT
scheme
PPT
object oriented-programming
PPT
Introduction to trees
PPT
Tree basics
PPT
1.2 steps and functionalities
PDF
Two dimensional array
PPT
Chomsky & Greibach Normal Forms
PPT
Variants of Turing Machine
PPT
Shell sort
PPT
Partial compute function
PPTX
Ambiguty
universality
best first-sort
language construction
packaging procedures_and_state
Array1
evaluation rules
Inference rulesproofmethods
scheme
object oriented-programming
Introduction to trees
Tree basics
1.2 steps and functionalities
Two dimensional array
Chomsky & Greibach Normal Forms
Variants of Turing Machine
Shell sort
Partial compute function
Ambiguty
Ad

Similar to composing procedures (20)

PPTX
ForLoopandUserDefinedFunctions.pptx
PPTX
PYTHON-PROGRAMMING-UNIT-II (1).pptx
PDF
Matlab integration
PPTX
PYTHON-PROGRAMMING-UNIT-II.pptx gijtgjjgg jufgiju yrguhft hfgjutt jgg
PPTX
Derivatives and it’s simple applications
PPTX
Functions in advanced programming
PPT
lecture8-derivativerules-140925171214-phpapp01.ppt
PPTX
derivativesanditssimpleapplications-160828144729.pptx
PPTX
lesson-3-2-ppt-function-notation GEN MATH
DOCX
B61301007 matlab documentation
PDF
Tutorial2
PPT
Bisection method in maths 4
PPT
Lecture 8 derivative rules
DOC
Functions
PPTX
functions
PPTX
1. Ch_1 SL_1_Intro to Matlab.pptx
PPTX
1. Week 1_ Functions and Evaluate Functions.pptx
PDF
Matlab differential
PDF
Week 6
PPTX
Amit user defined functions xi (2)
ForLoopandUserDefinedFunctions.pptx
PYTHON-PROGRAMMING-UNIT-II (1).pptx
Matlab integration
PYTHON-PROGRAMMING-UNIT-II.pptx gijtgjjgg jufgiju yrguhft hfgjutt jgg
Derivatives and it’s simple applications
Functions in advanced programming
lecture8-derivativerules-140925171214-phpapp01.ppt
derivativesanditssimpleapplications-160828144729.pptx
lesson-3-2-ppt-function-notation GEN MATH
B61301007 matlab documentation
Tutorial2
Bisection method in maths 4
Lecture 8 derivative rules
Functions
functions
1. Ch_1 SL_1_Intro to Matlab.pptx
1. Week 1_ Functions and Evaluate Functions.pptx
Matlab differential
Week 6
Amit user defined functions xi (2)

More from Rajendran (20)

PPT
Element distinctness lower bounds
PPT
Scheduling with Startup and Holding Costs
PPT
Divide and conquer surfing lower bounds
PPT
Red black tree
PPT
Hash table
PPT
Medians and order statistics
PPT
Proof master theorem
PPT
Recursion tree method
PPT
Recurrence theorem
PPT
Master method
PPT
Master method theorem
PPT
Hash tables
PPT
Lower bound
PPT
Master method theorem
PPT
Greedy algorithms
PPT
Longest common subsequences in Algorithm Analysis
PPT
Dynamic programming in Algorithm Analysis
PPT
Average case Analysis of Quicksort
PPT
Np completeness
PPT
computer languages
Element distinctness lower bounds
Scheduling with Startup and Holding Costs
Divide and conquer surfing lower bounds
Red black tree
Hash table
Medians and order statistics
Proof master theorem
Recursion tree method
Recurrence theorem
Master method
Master method theorem
Hash tables
Lower bound
Master method theorem
Greedy algorithms
Longest common subsequences in Algorithm Analysis
Dynamic programming in Algorithm Analysis
Average case Analysis of Quicksort
Np completeness
computer languages

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PDF
What if we spent less time fighting change, and more time building what’s rig...
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PDF
RMMM.pdf make it easy to upload and study
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Empowerment Technology for Senior High School Guide
PPTX
Lesson notes of climatology university.
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PDF
IGGE1 Understanding the Self1234567891011
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Trump Administration's workforce development strategy
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
Final Presentation General Medicine 03-08-2024.pptx
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
What if we spent less time fighting change, and more time building what’s rig...
A powerpoint presentation on the Revised K-10 Science Shaping Paper
Orientation - ARALprogram of Deped to the Parents.pptx
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
RMMM.pdf make it easy to upload and study
Chinmaya Tiranga quiz Grand Finale.pdf
UNIT III MENTAL HEALTH NURSING ASSESSMENT
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Empowerment Technology for Senior High School Guide
Lesson notes of climatology university.
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
IGGE1 Understanding the Self1234567891011
Final Presentation General Medicine 03-08-2024.pptx
Trump Administration's workforce development strategy
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
LDMMIA Reiki Yoga Finals Review Spring Summer

composing procedures

  • 1. 4.2 Composing procedures One way to divide a problem is to split it into steps where the output of the first step is the input to the second step, and the output of the second step is the solution to the problem. Each step can be defined by one procedure, and the two procedures can be combined to create one procedure that solves the problem. Figure 4.2 shows a composition of two functions, f and g. The output of f is used as the input to g. Figure 4.2: Composition. We can express this composition with the Scheme expression (g (f x)) where xis the input. The written order appears to be reversed from the picture in Figure 4.2. This is because we apply a procedure to the values of its subexpressions: the values of the inner subexpressions must be computed first, and then used as the inputs to the outer applications.
  • 2. So, the inner subexpression(f x) is evaluated first since the evaluation rule for the outer application expression is to first evaluate all the subexpressions. To define a procedure that implements the composed procedure we make x a parameter: (define fog (lambda (x) (g (f x)))) This defines fog as a procedure that takes one input and produces as output the composition of f and g applied to the input parameter. This works for any two procedures that both take a single input parameter. We can compose the square and cube procedures from Chapter 3: (define sixth-power (lambda (x) (cube (square x)))) Then, (sixth-power 2) evaluates to 64.
  • 3. 4.2.1 Procedures as inputs and outputs All the procedure inputs and outputs we have seen so far have been numbers. The subexpressions of an application can be any expression including a procedure. A higher-order procedure is a procedure that takes other procedures as inputs or that produces a procedure as its output. Higher-order procedures give us the ability to write procedures that behave differently based on the procedures that are passed in as inputs. We can create a generic composition procedure by making f and g parameters: (define fog (lambda (f g x) (g (f x)))) The fog procedure takes three parameters. The first two are both procedures that take one input. The third parameter is a value that can be the input to the first procedure. For example, (fog square cube 2) evaluates to 64, and (fog (lambda (x) (+ x 1)) square 2) evaluates to 9. In the second example, the first parameter is the procedure produced by the lambda expression (lambda (x) (+ x 1)). This procedure takes a number as input and produces as output that number plus one. We use a definition to name this procedure inc (short for increment): (define inc (lambda (x) (+ x 1))) A more useful composition procedure would separate the input value, x, from the composition.
  • 4. The fcompose procedure takes two procedures as inputs and produces as output a procedure that is their composition:1 (define fcompose (lambda (f g) (lambda (x) (g (f x))))) The body of the fcompose procedure is a lambda expression that makes a procedure. Hence, the result of applying fcompose to two procedures is not a simple value, but a procedure. The resulting procedure can then be applied to a value. Here are some examples using fcompose: Scheme output > (fcompose inc inc) # < procedure > > ((fcompose inc inc) 1) 3 > ((fcompose inc square) 2) 9 > ((fcompose square inc) 2) 5
  • 5. Exercise 4.1. For each expression, give the value to which the expression evaluates. Assume fcompose and inc are defined as above. •((fcompose square square) 3) •(fcompose (lambda (x) (* x 2)) (lambda (x) (/ x 2))) •((fcompose (lambda (x) (* x 2)) (lambda (x) (/ x 2))) 1120) •((fcompose (fcompose inc inc) inc) 2) Exercise 4.2. Suppose we define self-compose as a procedure that composes a procedure with itself: (define (self-compose f) (fcompose f f)) Explain how(((fcompose self- compose self-compose) inc) 1) is evaluated. Exercise 4.3. Define a procedure fcompose3 that takes three procedures as input, and produces as output a procedure that is the composition of the three input procedures. For example, ((fcompose3 abs inc square) -5) should evaluate to36. Define fcompose3 two different ways: once without using fcompose, and once using fcompose. Exercise 4.4. The fcompose procedure only works when both input procedures take one input. Define a f2compose procedure that composes two procedures where the first procedure takes two inputs, and the second procedure takes one input. For example, ((f2compose + abs) 3 -5) should evaluate to 2.
  • 6. Exercise 4.1. For each expression, give the value to which the expression evaluates. Assume fcompose and inc are defined as above. •((fcompose square square) 3) •(fcompose (lambda (x) (* x 2)) (lambda (x) (/ x 2))) •((fcompose (lambda (x) (* x 2)) (lambda (x) (/ x 2))) 1120) •((fcompose (fcompose inc inc) inc) 2) Exercise 4.2. Suppose we define self-compose as a procedure that composes a procedure with itself: (define (self-compose f) (fcompose f f)) Explain how(((fcompose self- compose self-compose) inc) 1) is evaluated. Exercise 4.3. Define a procedure fcompose3 that takes three procedures as input, and produces as output a procedure that is the composition of the three input procedures. For example, ((fcompose3 abs inc square) -5) should evaluate to36. Define fcompose3 two different ways: once without using fcompose, and once using fcompose. Exercise 4.4. The fcompose procedure only works when both input procedures take one input. Define a f2compose procedure that composes two procedures where the first procedure takes two inputs, and the second procedure takes one input. For example, ((f2compose + abs) 3 -5) should evaluate to 2.