SlideShare a Scribd company logo
Higher-Order Procedures (in Ruby) based on ‘Structure and Interpretation of Computer Programs’ (1985 MIT Press)  by Hal Abelson and Gerald Jay Sussman.  http://swiss.csail.mit.edu/classes/6.001/abelson-sussman-lectures/ Nathan Murray < [email_address] > v1.0  12/13/06  http://www.natemurray.com
legal The copy in this presentation is taken directly from Structure and Interpretation of Computer Programs by Hal Abelson and Gerald Jay Sussman (MIT Press, 1984; ISBN 0-262-01077-1). Specifically section 1.3 Formulating Abstractions with Higher-Order Procedures. There are a few paraphrases and additional examples added.  The main difference is that the code has been converted from Lisp to Ruby.  The full text of this book and accompanying video lectures can be found at: http://swiss.csail.mit.edu/classes/6.001/abelson-sussman-lectures/ The video lectures are copyright by Hal Abelson and Gerald Jay Sussman.  The video lectures, and in turn this document, are licensed under a Creative Commons License. http://creativecommons.org/licenses/by-sa/2.0/
•  Procedures are abstractions def  cube (a) a * a * a end
( 3  *  3  *  3 ) (x * x * x) (y * y * y) x 3
We need more than numbers for parameters Patterns indicate concepts Higher-Order Procedures Manipulating Procedures = Power
some examples Consider the following three procedures.
The first computes the sum of the integers from a through b: def  sum_integers (a, b) return   0   if  a > b a + sum_integers((a +  1 ), b) end sum_integers( 1 ,  10 )  #=> 55
The second computes the sum of the cubes of the integers in the given range: def  sum_cubes (a, b) return   0   if  a > b cube(a) + sum_cubes((a +  1 ), b) end sum_cubes( 1 ,  3 )  #=> 36
The third computes the sum of a sequence of terms in the series: def  pi_sum (a, b) return   0   if  a > b ( 1.0  / ((a +  2 ) * a)) + (pi_sum((a +  4 ), b)) end pi_sum( 1 ,  1000 ) *  8   #=> 3.13959265558978 which converges to  π/8  (very slowly)
a pattern... def  sum_integers (a, b) return   0   if  a > b a + sum_integers((a +  1 ), b) end def  sum_cubes (a, b) return   0   if  a > b cube(a) + sum_cubes((a +  1 ), b) end def  pi_sum (a, b) return   0   if  a > b ( 1.0  / ((a +  2 ) * a)) + (pi_sum((a +  4 ), b)) end
template def  <name> (a, b) return   0   if  a > b <term>(a) + <name>(< next >(a), b) end
summation
def  <name> (a, b) return   0   if  a > b <term>(a) + <name>(< next >(a), b) end def  sum (term, a, the_next, b) return   0   if  a > b term.call(a) + sum(term, the_next.call(a), the_next, b) end
sum cubes def  inc (n) n +  1 end def  sum_cubes (a, b) cube =  self .method( :cube ).to_proc inc  =  self .method( :inc  ).to_proc sum(cube, a, inc, b) end sum_cubes( 1 ,  3 )  #=> 36
sum integers def  identity (x) x end def  sum_integers (a, b) id  =  self .method( :identity ).to_proc inc =  self .method( :inc   ).to_proc sum(id, a, inc, b) end sum_integers( 1 ,  10 )  #=> 55
π  sum def  pi_term (x) ( 1.0  / (x * (x+ 2 ))) end def  pi_next (x) (x +  4 ) end def  pi_sum (a, b) term =  self .method( :pi_term ).to_proc nex  =  self .method( :pi_next ).to_proc sum(term, a, nex, b) end pi_sum( 1 ,  1000 ) *  8   #=> 3.13959265558978 λ
λ  def  pi_sum (a, b) sum(  , a, , b ) end lambda { | x | ( 1.0  / (x * (x+ 2 ))) } lambda { | x | (x +  4 ) }
another example def  even? (i) i %  2  ==  0 end def  filter_evens (list) new_list = [] list.each  do  | element | new_list << element  if  even?(element) end new_list end filter_evens( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] )  #=> [2, 4, 6, 8]
returning procedures def  make_filter (predicate) lambda   do  | list | new_list = [] list.each  do  | element | new_list << element  if  predicate.call(element) end new_list end end filter_odds = make_filter(  lambda {| i | i %  2  !=  0 } ) filter_odds.call(list)  #=> [1, 3, 5, 7, 9]
returning procedures filter_ths = make_filter( lambda   do  | i | i.ordinal =~  / th$ /  ?  true  :  false end ) filter_ths.call(list)  #=> [4, 5, 6, 7, 8, 9, 10] require   ' facet/integer/ordinal ' 10 .ordinal  #=> &quot;10th&quot;
wrap-up identify abstractions abstraction = power be appropriate

More Related Content

PPTX
Alegebra Powers Substitution
PPTX
Application of Derivative 5
PDF
Day 1 examples u8f13
PDF
Day 1 examples u6w14
PPTX
Fields in cryptography
PPTX
Addition and Subtraction of Functions
PPTX
Math example
PDF
Hardcore functional programming
Alegebra Powers Substitution
Application of Derivative 5
Day 1 examples u8f13
Day 1 examples u6w14
Fields in cryptography
Addition and Subtraction of Functions
Math example
Hardcore functional programming

What's hot (18)

PDF
Chpt13 laplacetransformsmatlab
PPT
Composite functions
PPTX
2.3 slopes and difference quotient t
PPT
Equation of a straight line y b = m(x a)
PPT
mathemathics + Straight line equation
DOCX
เซต
PPT
Operations on Functions
PDF
Common symbols used in set theory
PPT
F(x) terminology
PPT
Pythagorean theorem and distance formula
PDF
AP Calculus Slides October 17, 2007
PPTX
2.4 operations on functions
TXT
Program python
PDF
Iit jee question_paper
PDF
Operations With Functions May 25 2009
PPTX
Variables, Expressions, and the Distributive Property
PPT
Chpt13 laplacetransformsmatlab
Composite functions
2.3 slopes and difference quotient t
Equation of a straight line y b = m(x a)
mathemathics + Straight line equation
เซต
Operations on Functions
Common symbols used in set theory
F(x) terminology
Pythagorean theorem and distance formula
AP Calculus Slides October 17, 2007
2.4 operations on functions
Program python
Iit jee question_paper
Operations With Functions May 25 2009
Variables, Expressions, and the Distributive Property
Ad

Viewers also liked (16)

PPTX
CST Review_Atoms and Atomic Structure
PPT
Constant strain triangular
PPTX
An Introduction to Sale Procedures Orders
PDF
Cascading - A Java Developer’s Companion to the Hadoop World
PPTX
Finite element method
PPT
Standard Operating Procedures
PPTX
SEVEN STEPS TO CUSTOMER SUCCESS AT SCALE
PPTX
The Customer Success Metrics That Matter
PPT
SOP (Standard Operational Procedure)
PPTX
Customer Success Strategy Template
DOCX
Standard Operating Procedure (SOP) for Information Technology (IT) Operations
PPTX
The 5 Must Have Customer Success Processes
PPTX
Sale of goods act, 1930
PPTX
Human Resource Management
PPTX
Customer Success Management ( CSM ) Org Structures by Gainsight
CST Review_Atoms and Atomic Structure
Constant strain triangular
An Introduction to Sale Procedures Orders
Cascading - A Java Developer’s Companion to the Hadoop World
Finite element method
Standard Operating Procedures
SEVEN STEPS TO CUSTOMER SUCCESS AT SCALE
The Customer Success Metrics That Matter
SOP (Standard Operational Procedure)
Customer Success Strategy Template
Standard Operating Procedure (SOP) for Information Technology (IT) Operations
The 5 Must Have Customer Success Processes
Sale of goods act, 1930
Human Resource Management
Customer Success Management ( CSM ) Org Structures by Gainsight
Ad

Similar to Higher Order Procedures (in Ruby) (20)

PPTX
Pycon 2011 talk (may not be final, note)
PPTX
PyCon 2011 talk - ngram assembly with Bloom filters
PPTX
GE8151 Problem Solving and Python Programming
DOCX
B61301007 matlab documentation
PDF
Basic R Data Manipulation
PPT
Matlab1
PPTX
Python programming workshop session 3
ODP
Scala as a Declarative Language
PPT
Matlab Basic Tutorial
PPT
Grokking Monads in Scala
PDF
iRODS Rule Language Cheat Sheet
PDF
Let’s Talk About Ruby
PPT
User deined functions cbse class xii computer science
PDF
Python Puzzlers
PPT
Stacks.ppt
PPT
Stacks.ppt
PDF
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
PPT
MATLAB-Introd.ppt
PDF
Fp in scala part 2
Pycon 2011 talk (may not be final, note)
PyCon 2011 talk - ngram assembly with Bloom filters
GE8151 Problem Solving and Python Programming
B61301007 matlab documentation
Basic R Data Manipulation
Matlab1
Python programming workshop session 3
Scala as a Declarative Language
Matlab Basic Tutorial
Grokking Monads in Scala
iRODS Rule Language Cheat Sheet
Let’s Talk About Ruby
User deined functions cbse class xii computer science
Python Puzzlers
Stacks.ppt
Stacks.ppt
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
MATLAB-Introd.ppt
Fp in scala part 2

Recently uploaded (20)

PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Getting Started with Data Integration: FME Form 101
PDF
Approach and Philosophy of On baking technology
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
A Presentation on Artificial Intelligence
PPTX
Tartificialntelligence_presentation.pptx
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Programs and apps: productivity, graphics, security and other tools
Unlocking AI with Model Context Protocol (MCP)
Assigned Numbers - 2025 - Bluetooth® Document
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
SOPHOS-XG Firewall Administrator PPT.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Getting Started with Data Integration: FME Form 101
Approach and Philosophy of On baking technology
Spectral efficient network and resource selection model in 5G networks
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
A Presentation on Artificial Intelligence
Tartificialntelligence_presentation.pptx
A comparative analysis of optical character recognition models for extracting...
Agricultural_Statistics_at_a_Glance_2022_0.pdf

Higher Order Procedures (in Ruby)

  • 1. Higher-Order Procedures (in Ruby) based on ‘Structure and Interpretation of Computer Programs’ (1985 MIT Press) by Hal Abelson and Gerald Jay Sussman. http://swiss.csail.mit.edu/classes/6.001/abelson-sussman-lectures/ Nathan Murray < [email_address] > v1.0 12/13/06 http://www.natemurray.com
  • 2. legal The copy in this presentation is taken directly from Structure and Interpretation of Computer Programs by Hal Abelson and Gerald Jay Sussman (MIT Press, 1984; ISBN 0-262-01077-1). Specifically section 1.3 Formulating Abstractions with Higher-Order Procedures. There are a few paraphrases and additional examples added. The main difference is that the code has been converted from Lisp to Ruby. The full text of this book and accompanying video lectures can be found at: http://swiss.csail.mit.edu/classes/6.001/abelson-sussman-lectures/ The video lectures are copyright by Hal Abelson and Gerald Jay Sussman. The video lectures, and in turn this document, are licensed under a Creative Commons License. http://creativecommons.org/licenses/by-sa/2.0/
  • 3. • Procedures are abstractions def cube (a) a * a * a end
  • 4. ( 3 * 3 * 3 ) (x * x * x) (y * y * y) x 3
  • 5. We need more than numbers for parameters Patterns indicate concepts Higher-Order Procedures Manipulating Procedures = Power
  • 6. some examples Consider the following three procedures.
  • 7. The first computes the sum of the integers from a through b: def sum_integers (a, b) return 0 if a > b a + sum_integers((a + 1 ), b) end sum_integers( 1 , 10 ) #=> 55
  • 8. The second computes the sum of the cubes of the integers in the given range: def sum_cubes (a, b) return 0 if a > b cube(a) + sum_cubes((a + 1 ), b) end sum_cubes( 1 , 3 ) #=> 36
  • 9. The third computes the sum of a sequence of terms in the series: def pi_sum (a, b) return 0 if a > b ( 1.0 / ((a + 2 ) * a)) + (pi_sum((a + 4 ), b)) end pi_sum( 1 , 1000 ) * 8 #=> 3.13959265558978 which converges to π/8 (very slowly)
  • 10. a pattern... def sum_integers (a, b) return 0 if a > b a + sum_integers((a + 1 ), b) end def sum_cubes (a, b) return 0 if a > b cube(a) + sum_cubes((a + 1 ), b) end def pi_sum (a, b) return 0 if a > b ( 1.0 / ((a + 2 ) * a)) + (pi_sum((a + 4 ), b)) end
  • 11. template def <name> (a, b) return 0 if a > b <term>(a) + <name>(< next >(a), b) end
  • 13. def <name> (a, b) return 0 if a > b <term>(a) + <name>(< next >(a), b) end def sum (term, a, the_next, b) return 0 if a > b term.call(a) + sum(term, the_next.call(a), the_next, b) end
  • 14. sum cubes def inc (n) n + 1 end def sum_cubes (a, b) cube = self .method( :cube ).to_proc inc = self .method( :inc ).to_proc sum(cube, a, inc, b) end sum_cubes( 1 , 3 ) #=> 36
  • 15. sum integers def identity (x) x end def sum_integers (a, b) id = self .method( :identity ).to_proc inc = self .method( :inc ).to_proc sum(id, a, inc, b) end sum_integers( 1 , 10 ) #=> 55
  • 16. π sum def pi_term (x) ( 1.0 / (x * (x+ 2 ))) end def pi_next (x) (x + 4 ) end def pi_sum (a, b) term = self .method( :pi_term ).to_proc nex = self .method( :pi_next ).to_proc sum(term, a, nex, b) end pi_sum( 1 , 1000 ) * 8 #=> 3.13959265558978 λ
  • 17. λ  def pi_sum (a, b) sum( , a, , b ) end lambda { | x | ( 1.0 / (x * (x+ 2 ))) } lambda { | x | (x + 4 ) }
  • 18. another example def even? (i) i % 2 == 0 end def filter_evens (list) new_list = [] list.each do | element | new_list << element if even?(element) end new_list end filter_evens( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] ) #=> [2, 4, 6, 8]
  • 19. returning procedures def make_filter (predicate) lambda do | list | new_list = [] list.each do | element | new_list << element if predicate.call(element) end new_list end end filter_odds = make_filter( lambda {| i | i % 2 != 0 } ) filter_odds.call(list) #=> [1, 3, 5, 7, 9]
  • 20. returning procedures filter_ths = make_filter( lambda do | i | i.ordinal =~ / th$ / ? true : false end ) filter_ths.call(list) #=> [4, 5, 6, 7, 8, 9, 10] require ' facet/integer/ordinal ' 10 .ordinal #=> &quot;10th&quot;
  • 21. wrap-up identify abstractions abstraction = power be appropriate