lesson 2.1 using a template - northeastern university 2.1 using a... · recipe for using a template...

25
Design Strategies 2: Using a template CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.1 © Mitchell Wand, 2012-2014 This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. 1

Upload: others

Post on 10-Apr-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

NextSteps

• Study02-1-book-receipts.rktand02-2-traffic-light.rktintheExamplesfolder.– Besuretofinishtheprevious-stateexamplein02-2-traffic-light.rkt

• Ifyouhavequestionsorcommentsaboutthislesson,postthemonthediscussionboard.

• DotheGuidedPractices• Goontothenextlesson.

25