SlideShare une entreprise Scribd logo
1  sur  20
A Warmup Problem CS 5010 Program Design Paradigms “Bootcamp” Week 03, Lesson 1 TexPoint fonts used in EMF.  Read the TexPoint manual before you delete this box.: AAA 1
A simple job-interview problem Given a list of digits, compute its value as a decimal number.
Lists of Digits A Digit is one of  "0" | "1" | "2" | ... | "9" A List of Digits (LOD) is one of: -- empty -- (cons Digit LOD)
Shortcut list notation (list 11 22 33)   = (cons 11 (cons 22 (cons 33 empty))) (list 22) = (cons 22 empty) (list) = empty
lod->number lod2number : LOD -> Number Given a LOD, produce the number it represents (lod2number (list 3)) = 3 (lod2number (list 2 1)) = 21 (lod2number (list 7 1 4)) = 714 (lod2number (list 7 14)) = ?? argument is not an LOD, so behavior is unspecified!
Template for List functions ;; list-fn : LOX -> ?? (define (list-fnlst)   (cond     [(empty? lst) ...]     [else (... (first lst)                (list-fn (rest lst)))]))
Template for List functions ;; list-fn : LOX -> ?? (define (list-fnlst)   (cond     [(empty? lst) ...]     [else (... (first lst)                (list-fn (rest lst)))])) What's the answer for the empty list? If we knew the answer for the rest of the list, and we knew the first of the list, how could we combine them to get the answer for the whole list?
lod2number ;; lod2number : LOD -> Number (define (lod2numberlst)   (cond     [(empty? lst) ...]     [else (... (first lst)                (lod2number (rest lst)))])) What's the answer for the empty list? If we knew the answer for the rest of the list, and we knew the first of the list, how could we combine them to get the answer for the whole list?
Hmm, let's  study the examples: (lod2number (list 3)) = 3 (lod2number (list 2 3))    = 2*10 + 3   = 2*10 + (lod2number (list 3)) (lod2number (list 4 2 3))   = 4*100 + 2*10 + 3   = 4*100 + (lod2number (list 2 3)) 10^2 a list of length 2
Hmm, let's  study the examples: (lod2number (list 4 2 3))   = 4*100 + 2*10 + 3   = 4*100 + (lod2number (list 2 3))   = 4*10^2        + (lod2number (list 2 3)) 	= 4*(10^(length (list 2 3)))       + (lod2number (list 2 3))
Conclusion So, if lst is nonempty, (lod2number lst) =   (+ (* (digit2number (first lst))         (power 10 (length (rest lst))))      (lod2number (rest lst))) What about empty? (lod2number empty) = 0 (length empty) = 0, so everything works.
lod2number ;; lod2number : LOD -> Number (define (lod2number lst)   (cond     [(empty? lst) 0]     [else (+ (* (digit2number (first lst))                 (power 10 (length (rest lst))))              (lod2number (rest lst))))])) ;; Add to wishlist: digit2number : Digit -> Number power : Number NonNegInt -> Number Given base and exponent, produces base^exponent
Let's work on wishlist ;; digit2number : Digit -> Number ;; strategy: structural decomposition on Digit (define (digit2number d)   (cond     [(string=? d "0") 0]     [(string=? d "1") 1]     [(string=? d "2") 2]     [(string=? d "3") 3]     [(string=? d "4") 4]     [(string=? d "5") 5]     [(string=? d "6") 6]     [(string=? d "7") 7]     [(string=? d "8") 8]     [(string=? d "9") 9]))
Next we'll work on  power ;; power : Number NonNegInt -> Number ;; Given base and exponent, produces base^exponent (check-expect (power 10 0) 1) (check-expect (power 10 1) 10) (check-expect (power 10 2) 100) (check-expect (power 10 3) 1000) (check-expect (power 2 1) 2) (check-expect (power 2 2) 4) (check-expect (power 2 3) 8)
Hmm, what about power? We can use structural decomposition on NonNegInt: ;; A NonNegInt is one of ;; -- 0 ;; -- (+ 1 NonNegInt)
Template for NonNegInt (define (f n)   (cond     [(zero? n) ...]     [else (... (f (- n 1)))]))
power (define (power base n)   (cond     [(zero? n) 1]     [else (* base (power base (- n 1)))]))
Ready to test! string=?: expects type <string> as 1st argument, given: 3; other arguments were: "0" Oops– what happened? I said:  A Digit is one of  "0" | "1" | "2" | ... | "9" But the tests were things like: (check-expect (lod2number (list 3)) 3) 3 is not a digit. "3" is a digit
Oops, our tests were wrong! How did we determine that it was the fault of the tests? Ans: we looked at the data definition! Fix: add “..” ’s: (check-expect (lod2number (list "3")) 3) (check-expect (lod2number (list "2" "1")) 21) (check-expect (lod2number (list "7" "1" "4")) 714)
Summary Follow the recipe! Use the template! it tells you form of the program it tells you what questions to ask for the blanks Study the examples! The wishlist helped us organize a program with several procedures.

Contenu connexe

Tendances

ECMAScript 6 and beyond
ECMAScript 6 and beyondECMAScript 6 and beyond
ECMAScript 6 and beyondFrancis Johny
 
Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)gekiaruj
 
Pragmatic metaprogramming
Pragmatic metaprogrammingPragmatic metaprogramming
Pragmatic metaprogrammingMårten Rånge
 
2015 02-18 xxx-literalconvertible
2015 02-18 xxx-literalconvertible2015 02-18 xxx-literalconvertible
2015 02-18 xxx-literalconvertibleTaketo Sano
 
The java language cheat sheet
The java language cheat sheetThe java language cheat sheet
The java language cheat sheetanand_study
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語ikdysfm
 
12X1 T05 01 inverse functions (2010)
12X1 T05 01 inverse functions (2010)12X1 T05 01 inverse functions (2010)
12X1 T05 01 inverse functions (2010)Nigel Simmons
 
Cheat sheet python3
Cheat sheet python3Cheat sheet python3
Cheat sheet python3sxw2k
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheetGil Cohen
 
The Table Method for Derivatives
The Table Method for DerivativesThe Table Method for Derivatives
The Table Method for DerivativesTyler Murphy
 

Tendances (20)

Matlab differential
Matlab differentialMatlab differential
Matlab differential
 
ECMAScript 6 and beyond
ECMAScript 6 and beyondECMAScript 6 and beyond
ECMAScript 6 and beyond
 
gfg
gfggfg
gfg
 
Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)
 
Pragmatic metaprogramming
Pragmatic metaprogrammingPragmatic metaprogramming
Pragmatic metaprogramming
 
Inverse Functions
Inverse FunctionsInverse Functions
Inverse Functions
 
2015 02-18 xxx-literalconvertible
2015 02-18 xxx-literalconvertible2015 02-18 xxx-literalconvertible
2015 02-18 xxx-literalconvertible
 
The java language cheat sheet
The java language cheat sheetThe java language cheat sheet
The java language cheat sheet
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
12X1 T05 01 inverse functions (2010)
12X1 T05 01 inverse functions (2010)12X1 T05 01 inverse functions (2010)
12X1 T05 01 inverse functions (2010)
 
Python Cheat Sheet
Python Cheat SheetPython Cheat Sheet
Python Cheat Sheet
 
Cheat sheet python3
Cheat sheet python3Cheat sheet python3
Cheat sheet python3
 
Boolean algebra
Boolean algebraBoolean algebra
Boolean algebra
 
Haskell 101
Haskell 101Haskell 101
Haskell 101
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheet
 
Java Cheat Sheet
Java Cheat SheetJava Cheat Sheet
Java Cheat Sheet
 
Talk Code
Talk CodeTalk Code
Talk Code
 
Java cheatsheet
Java cheatsheetJava cheatsheet
Java cheatsheet
 
The Table Method for Derivatives
The Table Method for DerivativesThe Table Method for Derivatives
The Table Method for Derivatives
 
Python_ 3 CheatSheet
Python_ 3 CheatSheetPython_ 3 CheatSheet
Python_ 3 CheatSheet
 

En vedette

Language Learning and Teaching
Language Learning and  TeachingLanguage Learning and  Teaching
Language Learning and TeachingAmutha Selvaraj
 
25 Teacher Resources
25 Teacher Resources25 Teacher Resources
25 Teacher Resourcesguestea1bda
 
Sketching Workshop with Product Design Team
Sketching Workshop with Product Design TeamSketching Workshop with Product Design Team
Sketching Workshop with Product Design TeamArganka Yahya
 
Memletics Learning Styles Inventory
Memletics Learning Styles InventoryMemletics Learning Styles Inventory
Memletics Learning Styles InventoryLarry Weas
 
Application of memletics and grasha riechmann learing style
Application of memletics and grasha riechmann learing styleApplication of memletics and grasha riechmann learing style
Application of memletics and grasha riechmann learing styleSar Lyna
 
Baby Time Fall 2013 Lyrics
Baby Time Fall 2013 LyricsBaby Time Fall 2013 Lyrics
Baby Time Fall 2013 LyricsCen Campbell
 
Issues Around Teaching Children A Language
Issues Around Teaching Children A LanguageIssues Around Teaching Children A Language
Issues Around Teaching Children A LanguageBishara Adam
 
Presentacion Del Taller En Ingles
Presentacion Del Taller En InglesPresentacion Del Taller En Ingles
Presentacion Del Taller En InglesClaudia
 
Using you tube in the esl classroom
Using you tube in the esl classroomUsing you tube in the esl classroom
Using you tube in the esl classroomthreekain
 
Stewie's family routine
Stewie's family routineStewie's family routine
Stewie's family routineMaylincita CG
 
Story no1. (present simple tense)
Story no1. (present simple tense)Story no1. (present simple tense)
Story no1. (present simple tense)pingpingmum
 
Teaching approches
Teaching approchesTeaching approches
Teaching approchesFatin Amira
 
6940128 teaching-english-to-children
6940128 teaching-english-to-children6940128 teaching-english-to-children
6940128 teaching-english-to-childrenHuynhPhungMyHuyen
 
Reading activities for EFL children
Reading activities for EFL childrenReading activities for EFL children
Reading activities for EFL childrenendlessjdm
 
Daily routine story
Daily routine storyDaily routine story
Daily routine storyMonicargtz
 
Classroom management in teaching english at primary schools
Classroom  management in teaching english at primary schoolsClassroom  management in teaching english at primary schools
Classroom management in teaching english at primary schoolsFatmanurKurkcu
 
Teaching English Language Learners in Primary and Elementary Classrooms
Teaching English Language Learners in Primary and Elementary ClassroomsTeaching English Language Learners in Primary and Elementary Classrooms
Teaching English Language Learners in Primary and Elementary ClassroomsChristine Morris
 
Teaching children english
Teaching children englishTeaching children english
Teaching children englishADRIANA BECKER
 

En vedette (20)

Language Learning and Teaching
Language Learning and  TeachingLanguage Learning and  Teaching
Language Learning and Teaching
 
25 Teacher Resources
25 Teacher Resources25 Teacher Resources
25 Teacher Resources
 
Sketching Workshop with Product Design Team
Sketching Workshop with Product Design TeamSketching Workshop with Product Design Team
Sketching Workshop with Product Design Team
 
Memletics Learning Styles Inventory
Memletics Learning Styles InventoryMemletics Learning Styles Inventory
Memletics Learning Styles Inventory
 
Application of memletics and grasha riechmann learing style
Application of memletics and grasha riechmann learing styleApplication of memletics and grasha riechmann learing style
Application of memletics and grasha riechmann learing style
 
Baby Time Fall 2013 Lyrics
Baby Time Fall 2013 LyricsBaby Time Fall 2013 Lyrics
Baby Time Fall 2013 Lyrics
 
Issues Around Teaching Children A Language
Issues Around Teaching Children A LanguageIssues Around Teaching Children A Language
Issues Around Teaching Children A Language
 
Ice breaker book
Ice breaker bookIce breaker book
Ice breaker book
 
Presentacion Del Taller En Ingles
Presentacion Del Taller En InglesPresentacion Del Taller En Ingles
Presentacion Del Taller En Ingles
 
Using you tube in the esl classroom
Using you tube in the esl classroomUsing you tube in the esl classroom
Using you tube in the esl classroom
 
Stewie's family routine
Stewie's family routineStewie's family routine
Stewie's family routine
 
Story no1. (present simple tense)
Story no1. (present simple tense)Story no1. (present simple tense)
Story no1. (present simple tense)
 
Teaching approches
Teaching approchesTeaching approches
Teaching approches
 
6940128 teaching-english-to-children
6940128 teaching-english-to-children6940128 teaching-english-to-children
6940128 teaching-english-to-children
 
Reading activities for EFL children
Reading activities for EFL childrenReading activities for EFL children
Reading activities for EFL children
 
Daily routine story
Daily routine storyDaily routine story
Daily routine story
 
Classroom management in teaching english at primary schools
Classroom  management in teaching english at primary schoolsClassroom  management in teaching english at primary schools
Classroom management in teaching english at primary schools
 
Practical ideas
Practical ideasPractical ideas
Practical ideas
 
Teaching English Language Learners in Primary and Elementary Classrooms
Teaching English Language Learners in Primary and Elementary ClassroomsTeaching English Language Learners in Primary and Elementary Classrooms
Teaching English Language Learners in Primary and Elementary Classrooms
 
Teaching children english
Teaching children englishTeaching children english
Teaching children english
 

Similaire à Lesson 01 A Warmup Problem

Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structureShakil Ahmed
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqPradeep Bisht
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdfAshaWankar1
 
An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL乐群 陈
 
関数プログラミングことはじめ in 福岡
関数プログラミングことはじめ in 福岡関数プログラミングことはじめ in 福岡
関数プログラミングことはじめ in 福岡Naoki Kitora
 
time_complexity_list_02_04_2024_22_pages.pdf
time_complexity_list_02_04_2024_22_pages.pdftime_complexity_list_02_04_2024_22_pages.pdf
time_complexity_list_02_04_2024_22_pages.pdfSrinivasaReddyPolamR
 
Scala kansai summit-2016
Scala kansai summit-2016Scala kansai summit-2016
Scala kansai summit-2016Naoki Kitora
 
‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptxRamiHarrathi1
 
関数プログラミングことはじめ revival
関数プログラミングことはじめ revival関数プログラミングことはじめ revival
関数プログラミングことはじめ revivalNaoki Kitora
 
Introduction To Lisp
Introduction To LispIntroduction To Lisp
Introduction To Lispkyleburton
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meetMario Fusco
 
PyLecture4 -Python Basics2-
PyLecture4 -Python Basics2-PyLecture4 -Python Basics2-
PyLecture4 -Python Basics2-Yoshiki Satotani
 
Finish the program below that does several bit-wise manipulations of.pdf
Finish the program below that does several bit-wise manipulations of.pdfFinish the program below that does several bit-wise manipulations of.pdf
Finish the program below that does several bit-wise manipulations of.pdffasttrackcomputersol
 
ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)Alexandru Radovici
 

Similaire à Lesson 01 A Warmup Problem (20)

Ch8b
Ch8bCh8b
Ch8b
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
 
Scheme language
Scheme languageScheme language
Scheme language
 
Ch8a
Ch8aCh8a
Ch8a
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conq
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdf
 
An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL
 
Ch3
Ch3Ch3
Ch3
 
Array
ArrayArray
Array
 
関数プログラミングことはじめ in 福岡
関数プログラミングことはじめ in 福岡関数プログラミングことはじめ in 福岡
関数プログラミングことはじめ in 福岡
 
time_complexity_list_02_04_2024_22_pages.pdf
time_complexity_list_02_04_2024_22_pages.pdftime_complexity_list_02_04_2024_22_pages.pdf
time_complexity_list_02_04_2024_22_pages.pdf
 
Scala kansai summit-2016
Scala kansai summit-2016Scala kansai summit-2016
Scala kansai summit-2016
 
‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx
 
関数プログラミングことはじめ revival
関数プログラミングことはじめ revival関数プログラミングことはじめ revival
関数プログラミングことはじめ revival
 
Introduction To Lisp
Introduction To LispIntroduction To Lisp
Introduction To Lisp
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
 
PyLecture4 -Python Basics2-
PyLecture4 -Python Basics2-PyLecture4 -Python Basics2-
PyLecture4 -Python Basics2-
 
Finish the program below that does several bit-wise manipulations of.pdf
Finish the program below that does several bit-wise manipulations of.pdfFinish the program below that does several bit-wise manipulations of.pdf
Finish the program below that does several bit-wise manipulations of.pdf
 
Resoluçãohaskell2
Resoluçãohaskell2Resoluçãohaskell2
Resoluçãohaskell2
 
ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)
 

Plus de Mitchell Wand

Site visit presentation 2012 12 14
Site visit presentation 2012 12 14Site visit presentation 2012 12 14
Site visit presentation 2012 12 14Mitchell Wand
 
Week 02 Lesson 02 Recursive Data Definitions
Week 02 Lesson 02 Recursive Data DefinitionsWeek 02 Lesson 02 Recursive Data Definitions
Week 02 Lesson 02 Recursive Data DefinitionsMitchell Wand
 
Week 03 Lesson 02 recursive data definitions everywhere
Week 03 Lesson 02 recursive data definitions everywhereWeek 03 Lesson 02 recursive data definitions everywhere
Week 03 Lesson 02 recursive data definitions everywhereMitchell Wand
 
Lesson 03 Arbitrarily Recursive Data Definitions
Lesson 03 Arbitrarily  Recursive Data DefinitionsLesson 03 Arbitrarily  Recursive Data Definitions
Lesson 03 Arbitrarily Recursive Data DefinitionsMitchell Wand
 
Lesson 04 Lists Of Lists Of Lists
Lesson 04 Lists Of Lists Of ListsLesson 04 Lists Of Lists Of Lists
Lesson 04 Lists Of Lists Of ListsMitchell Wand
 
Lesson 05 Analyzing Structures
Lesson 05 Analyzing StructuresLesson 05 Analyzing Structures
Lesson 05 Analyzing StructuresMitchell Wand
 

Plus de Mitchell Wand (7)

My favorite slides
My favorite slidesMy favorite slides
My favorite slides
 
Site visit presentation 2012 12 14
Site visit presentation 2012 12 14Site visit presentation 2012 12 14
Site visit presentation 2012 12 14
 
Week 02 Lesson 02 Recursive Data Definitions
Week 02 Lesson 02 Recursive Data DefinitionsWeek 02 Lesson 02 Recursive Data Definitions
Week 02 Lesson 02 Recursive Data Definitions
 
Week 03 Lesson 02 recursive data definitions everywhere
Week 03 Lesson 02 recursive data definitions everywhereWeek 03 Lesson 02 recursive data definitions everywhere
Week 03 Lesson 02 recursive data definitions everywhere
 
Lesson 03 Arbitrarily Recursive Data Definitions
Lesson 03 Arbitrarily  Recursive Data DefinitionsLesson 03 Arbitrarily  Recursive Data Definitions
Lesson 03 Arbitrarily Recursive Data Definitions
 
Lesson 04 Lists Of Lists Of Lists
Lesson 04 Lists Of Lists Of ListsLesson 04 Lists Of Lists Of Lists
Lesson 04 Lists Of Lists Of Lists
 
Lesson 05 Analyzing Structures
Lesson 05 Analyzing StructuresLesson 05 Analyzing Structures
Lesson 05 Analyzing Structures
 

Lesson 01 A Warmup Problem

  • 1. A Warmup Problem CS 5010 Program Design Paradigms “Bootcamp” Week 03, Lesson 1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA 1
  • 2. A simple job-interview problem Given a list of digits, compute its value as a decimal number.
  • 3. Lists of Digits A Digit is one of "0" | "1" | "2" | ... | "9" A List of Digits (LOD) is one of: -- empty -- (cons Digit LOD)
  • 4. Shortcut list notation (list 11 22 33) = (cons 11 (cons 22 (cons 33 empty))) (list 22) = (cons 22 empty) (list) = empty
  • 5. lod->number lod2number : LOD -> Number Given a LOD, produce the number it represents (lod2number (list 3)) = 3 (lod2number (list 2 1)) = 21 (lod2number (list 7 1 4)) = 714 (lod2number (list 7 14)) = ?? argument is not an LOD, so behavior is unspecified!
  • 6. Template for List functions ;; list-fn : LOX -> ?? (define (list-fnlst) (cond [(empty? lst) ...] [else (... (first lst) (list-fn (rest lst)))]))
  • 7. Template for List functions ;; list-fn : LOX -> ?? (define (list-fnlst) (cond [(empty? lst) ...] [else (... (first lst) (list-fn (rest lst)))])) What's the answer for the empty list? If we knew the answer for the rest of the list, and we knew the first of the list, how could we combine them to get the answer for the whole list?
  • 8. lod2number ;; lod2number : LOD -> Number (define (lod2numberlst) (cond [(empty? lst) ...] [else (... (first lst) (lod2number (rest lst)))])) What's the answer for the empty list? If we knew the answer for the rest of the list, and we knew the first of the list, how could we combine them to get the answer for the whole list?
  • 9. Hmm, let's study the examples: (lod2number (list 3)) = 3 (lod2number (list 2 3)) = 2*10 + 3 = 2*10 + (lod2number (list 3)) (lod2number (list 4 2 3)) = 4*100 + 2*10 + 3 = 4*100 + (lod2number (list 2 3)) 10^2 a list of length 2
  • 10. Hmm, let's study the examples: (lod2number (list 4 2 3)) = 4*100 + 2*10 + 3 = 4*100 + (lod2number (list 2 3)) = 4*10^2 + (lod2number (list 2 3)) = 4*(10^(length (list 2 3))) + (lod2number (list 2 3))
  • 11. Conclusion So, if lst is nonempty, (lod2number lst) = (+ (* (digit2number (first lst)) (power 10 (length (rest lst)))) (lod2number (rest lst))) What about empty? (lod2number empty) = 0 (length empty) = 0, so everything works.
  • 12. lod2number ;; lod2number : LOD -> Number (define (lod2number lst) (cond [(empty? lst) 0] [else (+ (* (digit2number (first lst)) (power 10 (length (rest lst)))) (lod2number (rest lst))))])) ;; Add to wishlist: digit2number : Digit -> Number power : Number NonNegInt -> Number Given base and exponent, produces base^exponent
  • 13. Let's work on wishlist ;; digit2number : Digit -> Number ;; strategy: structural decomposition on Digit (define (digit2number d) (cond [(string=? d "0") 0] [(string=? d "1") 1] [(string=? d "2") 2] [(string=? d "3") 3] [(string=? d "4") 4] [(string=? d "5") 5] [(string=? d "6") 6] [(string=? d "7") 7] [(string=? d "8") 8] [(string=? d "9") 9]))
  • 14. Next we'll work on power ;; power : Number NonNegInt -> Number ;; Given base and exponent, produces base^exponent (check-expect (power 10 0) 1) (check-expect (power 10 1) 10) (check-expect (power 10 2) 100) (check-expect (power 10 3) 1000) (check-expect (power 2 1) 2) (check-expect (power 2 2) 4) (check-expect (power 2 3) 8)
  • 15. Hmm, what about power? We can use structural decomposition on NonNegInt: ;; A NonNegInt is one of ;; -- 0 ;; -- (+ 1 NonNegInt)
  • 16. Template for NonNegInt (define (f n) (cond [(zero? n) ...] [else (... (f (- n 1)))]))
  • 17. power (define (power base n) (cond [(zero? n) 1] [else (* base (power base (- n 1)))]))
  • 18. Ready to test! string=?: expects type <string> as 1st argument, given: 3; other arguments were: "0" Oops– what happened? I said: A Digit is one of "0" | "1" | "2" | ... | "9" But the tests were things like: (check-expect (lod2number (list 3)) 3) 3 is not a digit. "3" is a digit
  • 19. Oops, our tests were wrong! How did we determine that it was the fault of the tests? Ans: we looked at the data definition! Fix: add “..” ’s: (check-expect (lod2number (list "3")) 3) (check-expect (lod2number (list "2" "1")) 21) (check-expect (lod2number (list "7" "1" "4")) 714)
  • 20. Summary Follow the recipe! Use the template! it tells you form of the program it tells you what questions to ask for the blanks Study the examples! The wishlist helped us organize a program with several procedures.

Notes de l'éditeur

  1. Welcome to Week 3, Lesson 1: A Warmup ProblemIn this lesson, we will combine some of the techniques we have seen in previous lessons to solve a slightly more complicated problem.