SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Higher-Order Procedures ,[object Object],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
[object Object],[object Object],[object Object],[object Object]
some examples ,[object Object]
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 ,[object Object],[object Object],[object Object]

Weitere ähnliche Inhalte

Was ist angesagt?

mathemathics + Straight line equation
mathemathics + Straight line equationmathemathics + Straight line equation
mathemathics + Straight line equation
eme87
 
Operations on Functions
Operations on FunctionsOperations on Functions
Operations on Functions
swartzje
 
Pythagorean theorem and distance formula
Pythagorean theorem and distance formulaPythagorean theorem and distance formula
Pythagorean theorem and distance formula
50147146
 
2.4 operations on functions
2.4 operations on functions2.4 operations on functions
2.4 operations on functions
hisema01
 
Operations With Functions May 25 2009
Operations With Functions May 25 2009Operations With Functions May 25 2009
Operations With Functions May 25 2009
ingroy
 
Variables, Expressions, and the Distributive Property
Variables, Expressions, and the Distributive PropertyVariables, Expressions, and the Distributive Property
Variables, Expressions, and the Distributive Property
leilanidediosboon
 

Was ist angesagt? (19)

Chpt13 laplacetransformsmatlab
Chpt13 laplacetransformsmatlabChpt13 laplacetransformsmatlab
Chpt13 laplacetransformsmatlab
 
Composite functions
Composite functionsComposite functions
Composite functions
 
2.3 slopes and difference quotient t
2.3 slopes and difference quotient t2.3 slopes and difference quotient t
2.3 slopes and difference quotient t
 
Equation of a straight line y b = m(x a)
Equation of a straight line y   b = m(x a)Equation of a straight line y   b = m(x a)
Equation of a straight line y b = m(x a)
 
Formula1
Formula1Formula1
Formula1
 
mathemathics + Straight line equation
mathemathics + Straight line equationmathemathics + Straight line equation
mathemathics + Straight line equation
 
เซต
เซตเซต
เซต
 
Operations on Functions
Operations on FunctionsOperations on Functions
Operations on Functions
 
Common symbols used in set theory
Common symbols used in set theoryCommon symbols used in set theory
Common symbols used in set theory
 
F(x) terminology
F(x) terminologyF(x) terminology
F(x) terminology
 
Pythagorean theorem and distance formula
Pythagorean theorem and distance formulaPythagorean theorem and distance formula
Pythagorean theorem and distance formula
 
AP Calculus Slides October 17, 2007
AP Calculus Slides October 17, 2007AP Calculus Slides October 17, 2007
AP Calculus Slides October 17, 2007
 
2.4 operations on functions
2.4 operations on functions2.4 operations on functions
2.4 operations on functions
 
Program python
Program pythonProgram python
Program python
 
133467 p3a2
133467 p3a2133467 p3a2
133467 p3a2
 
Iit jee question_paper
Iit jee question_paperIit jee question_paper
Iit jee question_paper
 
Operations With Functions May 25 2009
Operations With Functions May 25 2009Operations With Functions May 25 2009
Operations With Functions May 25 2009
 
Variables, Expressions, and the Distributive Property
Variables, Expressions, and the Distributive PropertyVariables, Expressions, and the Distributive Property
Variables, Expressions, and the Distributive Property
 
Vertex
VertexVertex
Vertex
 

Andere mochten auch

CST Review_Atoms and Atomic Structure
CST Review_Atoms and Atomic StructureCST Review_Atoms and Atomic Structure
CST Review_Atoms and Atomic Structure
rrichards2
 
Standard Operating Procedures
Standard Operating ProceduresStandard Operating Procedures
Standard Operating Procedures
biinoida
 
SOP (Standard Operational Procedure)
SOP (Standard Operational Procedure)SOP (Standard Operational Procedure)
SOP (Standard Operational Procedure)
Sri Minuty Interest
 
Standard Operating Procedure (SOP) for Information Technology (IT) Operations
Standard Operating Procedure (SOP) for Information Technology (IT) OperationsStandard Operating Procedure (SOP) for Information Technology (IT) Operations
Standard Operating Procedure (SOP) for Information Technology (IT) Operations
Ronald Bartels
 
Sale of goods act, 1930
Sale of goods act, 1930Sale of goods act, 1930
Sale of goods act, 1930
surjeet tomar
 

Andere mochten auch (16)

CST Review_Atoms and Atomic Structure
CST Review_Atoms and Atomic StructureCST Review_Atoms and Atomic Structure
CST Review_Atoms and Atomic Structure
 
Constant strain triangular
Constant strain triangular Constant strain triangular
Constant strain triangular
 
An Introduction to Sale Procedures Orders
An Introduction to Sale Procedures OrdersAn Introduction to Sale Procedures Orders
An Introduction to Sale Procedures Orders
 
Cascading - A Java Developer’s Companion to the Hadoop World
Cascading - A Java Developer’s Companion to the Hadoop WorldCascading - A Java Developer’s Companion to the Hadoop World
Cascading - A Java Developer’s Companion to the Hadoop World
 
Finite element method
Finite element methodFinite element method
Finite element method
 
Standard Operating Procedures
Standard Operating ProceduresStandard Operating Procedures
Standard Operating Procedures
 
SEVEN STEPS TO CUSTOMER SUCCESS AT SCALE
SEVEN STEPS TO CUSTOMER SUCCESS AT SCALESEVEN STEPS TO CUSTOMER SUCCESS AT SCALE
SEVEN STEPS TO CUSTOMER SUCCESS AT SCALE
 
The Customer Success Metrics That Matter
The Customer Success Metrics That MatterThe Customer Success Metrics That Matter
The Customer Success Metrics That Matter
 
SOP (Standard Operational Procedure)
SOP (Standard Operational Procedure)SOP (Standard Operational Procedure)
SOP (Standard Operational Procedure)
 
Customer Success Strategy Template
Customer Success Strategy TemplateCustomer Success Strategy Template
Customer Success Strategy Template
 
Standard Operating Procedure (SOP) for Information Technology (IT) Operations
Standard Operating Procedure (SOP) for Information Technology (IT) OperationsStandard Operating Procedure (SOP) for Information Technology (IT) Operations
Standard Operating Procedure (SOP) for Information Technology (IT) Operations
 
The 5 Must Have Customer Success Processes
The 5 Must Have Customer Success ProcessesThe 5 Must Have Customer Success Processes
The 5 Must Have Customer Success Processes
 
Sop
SopSop
Sop
 
Sale of goods act, 1930
Sale of goods act, 1930Sale of goods act, 1930
Sale of goods act, 1930
 
Human Resource Management
Human Resource ManagementHuman Resource Management
Human Resource Management
 
Customer Success Management ( CSM ) Org Structures by Gainsight
Customer Success Management ( CSM ) Org Structures by GainsightCustomer Success Management ( CSM ) Org Structures by Gainsight
Customer Success Management ( CSM ) Org Structures by Gainsight
 

Ähnlich wie Higher Order Procedures (in Ruby)

Pycon 2011 talk (may not be final, note)
Pycon 2011 talk (may not be final, note)Pycon 2011 talk (may not be final, note)
Pycon 2011 talk (may not be final, note)
c.titus.brown
 
PyCon 2011 talk - ngram assembly with Bloom filters
PyCon 2011 talk - ngram assembly with Bloom filtersPyCon 2011 talk - ngram assembly with Bloom filters
PyCon 2011 talk - ngram assembly with Bloom filters
c.titus.brown
 
Let’s Talk About Ruby
Let’s Talk About RubyLet’s Talk About Ruby
Let’s Talk About Ruby
Ian Bishop
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
riue
 

Ähnlich wie Higher Order Procedures (in Ruby) (20)

Pycon 2011 talk (may not be final, note)
Pycon 2011 talk (may not be final, note)Pycon 2011 talk (may not be final, note)
Pycon 2011 talk (may not be final, note)
 
PyCon 2011 talk - ngram assembly with Bloom filters
PyCon 2011 talk - ngram assembly with Bloom filtersPyCon 2011 talk - ngram assembly with Bloom filters
PyCon 2011 talk - ngram assembly with Bloom filters
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
B61301007 matlab documentation
B61301007 matlab documentationB61301007 matlab documentation
B61301007 matlab documentation
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
 
Array
ArrayArray
Array
 
Matlab1
Matlab1Matlab1
Matlab1
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
 
Matlab Basic Tutorial
Matlab Basic TutorialMatlab Basic Tutorial
Matlab Basic Tutorial
 
Grokking Monads in Scala
Grokking Monads in ScalaGrokking Monads in Scala
Grokking Monads in Scala
 
iRODS Rule Language Cheat Sheet
iRODS Rule Language Cheat SheetiRODS Rule Language Cheat Sheet
iRODS Rule Language Cheat Sheet
 
Let’s Talk About Ruby
Let’s Talk About RubyLet’s Talk About Ruby
Let’s Talk About Ruby
 
Python Puzzlers
Python PuzzlersPython Puzzlers
Python Puzzlers
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
 
MATLAB-Introd.ppt
MATLAB-Introd.pptMATLAB-Introd.ppt
MATLAB-Introd.ppt
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

Higher Order Procedures (in Ruby)

  • 1.
  • 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.
  • 6.
  • 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.