SlideShare une entreprise Scribd logo
1  sur  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]

Contenu connexe

Tendances

Chpt13 laplacetransformsmatlab
Chpt13 laplacetransformsmatlabChpt13 laplacetransformsmatlab
Chpt13 laplacetransformsmatlabJason Harvey
 
Composite functions
Composite functionsComposite functions
Composite functionsShaun Wilson
 
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 tmath260
 
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)Shaun Wilson
 
mathemathics + Straight line equation
mathemathics + Straight line equationmathemathics + Straight line equation
mathemathics + Straight line equationeme87
 
Operations on Functions
Operations on FunctionsOperations on Functions
Operations on Functionsswartzje
 
Common symbols used in set theory
Common symbols used in set theoryCommon symbols used in set theory
Common symbols used in set theorysmZaiN1
 
Pythagorean theorem and distance formula
Pythagorean theorem and distance formulaPythagorean theorem and distance formula
Pythagorean theorem and distance formula50147146
 
AP Calculus Slides October 17, 2007
AP Calculus Slides October 17, 2007AP Calculus Slides October 17, 2007
AP Calculus Slides October 17, 2007Darren Kuropatwa
 
2.4 operations on functions
2.4 operations on functions2.4 operations on functions
2.4 operations on functionshisema01
 
Iit jee question_paper
Iit jee question_paperIit jee question_paper
Iit jee question_paperRahulMishra774
 
Operations With Functions May 25 2009
Operations With Functions May 25 2009Operations With Functions May 25 2009
Operations With Functions May 25 2009ingroy
 
Variables, Expressions, and the Distributive Property
Variables, Expressions, and the Distributive PropertyVariables, Expressions, and the Distributive Property
Variables, Expressions, and the Distributive Propertyleilanidediosboon
 

Tendances (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
 

En vedette

CST Review_Atoms and Atomic Structure
CST Review_Atoms and Atomic StructureCST Review_Atoms and Atomic Structure
CST Review_Atoms and Atomic Structurerrichards2
 
Constant strain triangular
Constant strain triangular Constant strain triangular
Constant strain triangular rahul183
 
An Introduction to Sale Procedures Orders
An Introduction to Sale Procedures OrdersAn Introduction to Sale Procedures Orders
An Introduction to Sale Procedures OrdersSuzzanne Uhland
 
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 WorldCascading
 
Finite element method
Finite element methodFinite element method
Finite element methodMANISH RANJAN
 
Standard Operating Procedures
Standard Operating ProceduresStandard Operating Procedures
Standard Operating Proceduresbiinoida
 
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 SCALETotango
 
The Customer Success Metrics That Matter
The Customer Success Metrics That MatterThe Customer Success Metrics That Matter
The Customer Success Metrics That MatterOpsPanda
 
SOP (Standard Operational Procedure)
SOP (Standard Operational Procedure)SOP (Standard Operational Procedure)
SOP (Standard Operational Procedure)Sri Minuty Interest
 
Customer Success Strategy Template
Customer Success Strategy TemplateCustomer Success Strategy Template
Customer Success Strategy TemplateOpsPanda
 
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) OperationsRonald Bartels
 
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 ProcessesTotango
 
Sale of goods act, 1930
Sale of goods act, 1930Sale of goods act, 1930
Sale of goods act, 1930surjeet tomar
 
Human Resource Management
Human Resource ManagementHuman Resource Management
Human Resource ManagementSuresh Rajan
 
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 GainsightGainsight
 

En vedette (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
 

Similaire à 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 filtersc.titus.brown
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingMuthu Vinayagam
 
B61301007 matlab documentation
B61301007 matlab documentationB61301007 matlab documentation
B61301007 matlab documentationManchireddy Reddy
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data ManipulationChu An
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3Abdul Haseeb
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Languagevsssuresh
 
Grokking Monads in Scala
Grokking Monads in ScalaGrokking Monads in Scala
Grokking Monads in ScalaTim Dalton
 
iRODS Rule Language Cheat Sheet
iRODS Rule Language Cheat SheetiRODS Rule Language Cheat Sheet
iRODS Rule Language Cheat SheetSamuel Lampa
 
Let’s Talk About Ruby
Let’s Talk About RubyLet’s Talk About Ruby
Let’s Talk About RubyIan Bishop
 
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...Dr. Volkan OBAN
 
MATLAB-Introd.ppt
MATLAB-Introd.pptMATLAB-Introd.ppt
MATLAB-Introd.pptkebeAman
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2Hang Zhao
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)riue
 

Similaire à 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)
 

Dernier

What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 

Dernier (20)

What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 

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.