Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template...
Transcript of Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template...
![Page 1: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/1.jpg)
DesignStrategies2:Usingatemplate
CS5010ProgramDesignParadigms“Bootcamp”Lesson2.1
©MitchellWand,2012-2014ThisworkislicensedunderaCreative Commons Attribution-NonCommercial 4.0 International License. 1
![Page 2: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/2.jpg)
Generalization
OverConstants
OverExpressions
OverContexts
OverDataRepresentations
OverMethodImplementations
MixedData
DataRepresentations
Basics
RecursiveData
FunctionalData
Objects&Classes
Stateful Objects
DesignStrategies
Combinesimplerfunctions
Useatemplate
Callamoregeneralfunction
CommunicateviaState
Module02
2
DivideintoCases
![Page 3: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/3.jpg)
Generalization
OverConstants
OverExpressions
OverContexts
OverDataRepresentations
OverMethodImplementations
MixedData
DataRepresentations
Basics
RecursiveData
FunctionalData
Objects&Classes
Stateful Objects
DesignStrategies
Combinesimplerfunctions
Useatemplate
Callamoregeneralfunction
CommunicateviaState
Lesson2.1
3
DivideintoCases
![Page 4: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/4.jpg)
Introduction
• Inthislesson,wewillshowhowtotakeapartnon-scalardatausingthedestructortemplateforthattypeofdata.
• Thisisthestrategyyouwilluseforthevastmajorityofyourfunctions.
4
![Page 5: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/5.jpg)
Let'sseewhereweare
TheFunction DesignRecipe
1.DataDesign
2.ContractandPurposeStatement
3. ExamplesandTests
4.DesignStrategy
5.FunctionDefinition
6.Program Review
5
TheSixPrinciplesofthiscourse
1.ProgrammingisaPeopleDiscipline
2.RepresentInformationasData;InterpretDataasInformation
3. Programsshouldconsistoffunctionsandmethodsthatconsumeandproducevalues
4.DesignFunctions Systematically
5.DesignSystemsIteratively
6.Passvalueswhenyoucan,sharestateonlywhenyoumust.
DesignStrategies
1.Combine simplerfunctions
2. Usetemplatefor<datadef>on<vble>
3.Divideintocaseson<condition>
4.UseHOF<mapfn>on<vble>
5.Callamoregeneralfunction
![Page 6: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/6.jpg)
Useadestructortemplate
• Usedwhentheproblemcanbesolvedbyexaminingapieceofnon-scalardata.
• Slogan:
Theshapeofthedatadeterminestheshapeof
theprogram.6
![Page 7: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/7.jpg)
Whatdoesitmeanto“examine”apieceofdata?
• Ifthedataiscompounddata,thismeansextractingitsfields.
• Ifthedataisitemizationdata,thismeansdeterminingwhichvariantthedatais.
• Ifthedataismixeddata,thismeansdeterminingwhichvariantthedatais,andthenextractingitsfields,ifany.
• Everydatadefinitionincludesatemplatethatshowshowthisexaminationprocessistobeorganized.
• Writingafunctionusingatemplateisaccomplishedbyfillingintheblanksofthetemplate.– Definitionof"fillingintheblank"tocomeinSlide11.
7
![Page 8: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/8.jpg)
FromTemplatetoFunctionDefinition
RecipeforUsingaTemplate1.Make acopyofthetemplateanduncommentit2.Fill inthefunctionnameandaddmoreargumentsifneeded3.Thestrategyis“Usetemplatefor<datadef>on<vble>,”where<datadef>isthekindofdatayouaretakingapart,and<vble>isthevariablewhosevalueyouarelookingat.4.Fillintheblanksinthetemplatebycombiningtheargumentsandthevaluesofthefieldsusingsimplerfunctions.
8
![Page 9: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/9.jpg)
Example:book-receipts;; book-receipts : Book NonNegInt -> NonNegInt;; GIVEN: a Book and the number of copies sold;; RETURNS: the total receipts from the sales of the ;; given book. Ignores the number of copies on hand.;; EXAMPLE:;; (book-receipts ;; (make-book "Felleisen" "HtdP2" 13 2795) 100);; = 279500
9
Todothis,we’llneedtolookinside theBooktoseeitsprice,sowe’llusethe
Booktemplate
![Page 10: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/10.jpg)
1.Makeacopyofthetemplateanduncommentit
(define (book-fn b)(...
(book-author b)(book-title b)(book-on-hand b)(book-price b)))
10
![Page 11: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/11.jpg)
2.Fillinthefunctionnameandaddmoreargumentsifneeded
(define (book-receipts b sales)(...
(book-author b)(book-title b)(book-on-hand b)(book-price b)))
11
![Page 12: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/12.jpg)
3.Writedownthestrategy
;; STRATEGY: Use template for Book on b.(define (book-receipts b sales)(...
(book-author b)(book-title b)(book-on-hand b)(book-price b)))
12
![Page 13: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/13.jpg)
4.Fillintheblanksinthetemplate
;; STRATEGY: Use template for Book on b.(define (book-receipts b sales)(* (book-price b) sales))
Thingswedidn’tuse:(book-author b)(book-title b)(book-on-hand b)
That’sOK!
13
Wesaid:“4.Fillintheblanksinthetemplatebycombining theargumentsandthevaluesofthefieldsusingsimpler functions.”
![Page 14: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/14.jpg)
Example:nextstateoftrafficlight;; DATA DEFINITION:;; a TrafficLightState (TLState) is one of;; -- "red";; -- "yellow" ;; -- "green";; INTERPRETATION: self-evident
14
![Page 15: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/15.jpg)
ContractandPurposeStatement;; next-state : TLState -> TLState;; GIVEN: a TLState;; RETURNS: the TLState that follows the given TLState;; EXAMPLES:;; (next-state "red") = "green";; (next-state "yellow") = "red";; (next-state "green") = "yellow"
15
![Page 16: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/16.jpg)
1.Makeacopyofthetemplateanduncommentit
(define (tls-fn state)(cond[(string=? state "red") ...][(string=? state "yellow") ...][(string=? state "green") ...]))
16
![Page 17: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/17.jpg)
2.Fillinthefunctionnameandaddmoreargumentsifneeded
(define (next-state state)(cond[(string=? state "red") ...][(string=? state "yellow") ...][(string=? state "green") ...]))
17
![Page 18: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/18.jpg)
3.Fillinthestrategy;; STRATEGY: Use template for TLState on state
(define (next-state state)(cond[(string=? state "red") ...][(string=? state "yellow") ...][(string=? state "green") ...]))
18
![Page 19: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/19.jpg)
4.Fillintheblanks;; STRATEGY: Use template for TLState on state
(define (next-state state)(cond[(string=? state "red") ...][(string=? state "yellow") ...][(string=? state "green") ...]))
19
Whatistheanswerfor“red”?
![Page 20: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/20.jpg)
4.Fillintheblanks;; STRATEGY: Use template for TLState on state
(define (next-state state)(cond[(string=? state "red") "green"][(string=? state "yellow") ...][(string=? state "green") ...]))
20
Whatistheanswerfor“red”?
Answer(fromexamples):“green”
![Page 21: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/21.jpg)
4.Fillintheblanks;; STRATEGY: Use template for TLState on state
(define (next-state state)(cond[(string=? state "red") "green"][(string=? state "yellow") "red"][(string=? state "green") ...]))
21
Whatistheanswerfor“yellow”?
Answer(fromexamples):“red”
![Page 22: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/22.jpg)
4.Fillintheblanks;; STRATEGY: Use template for TLState on state
(define (next-state state)(cond[(string=? state "red") "green"][(string=? state "yellow") "red"][(string=? state "green") "yellow"]))
22
Whatistheanswerfor“green”?
Answer(fromexamples):“yellow”
![Page 23: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/23.jpg)
Workingwithotherkindsofdata
• We'veseenhowtousetemplatesforcompounddataanditemizationdata
• Mixeddataworksthesameway.• Copythetemplate,uncommentit,andfillinthemissingpieces.That'sit!
• Ifyou'vethoughthardenoughaboutyourfunction,fillingintheblanksiseasy.
23
![Page 24: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/24.jpg)
Whatcanyouputintheblanks?
• Wesaid:Fillintheblanksinthetemplatebycombiningtheargumentsandthevaluesofthefieldsusingsimplerfunctions.
• Thismeans:– Youdon'thavetouseallofthefields– Youcanuseafieldtwice– Youdon'thavetousethefields"inorder"
• Butithastobesimple,asinLesson1.7
24
![Page 25: Lesson 2.1 Using a Template - Northeastern University 2.1 Using a... · Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fillin the function name and](https://reader030.fdocuments.in/reader030/viewer/2022040304/5e9648cecf3908219f6da0d7/html5/thumbnails/25.jpg)
NextSteps
• Study02-1-book-receipts.rktand02-2-traffic-light.rktintheExamplesfolder.– Besuretofinishtheprevious-stateexamplein02-2-traffic-light.rkt
• Ifyouhavequestionsorcommentsaboutthislesson,postthemonthediscussionboard.
• DotheGuidedPractices• Goontothenextlesson.
25