├── .gitattributes ├── .gitignore ├── 0-Prologue ├── rocket.png └── rocket.rkt ├── 1-Fixed-Size-Data ├── 01-Arithmetic │ ├── 01_The_Arithmetic_of_Numbers.rkt │ ├── 02_The_Arithmetic_of_Strings.rkt │ ├── 03_Mixing_It_Up.rkt │ ├── 04_Mixing_It_Up.rkt │ ├── 04_The_Arithmetic_of_Images.rkt │ ├── 05_The_Arithmetic_of_Booleans.rkt │ ├── 06_Mixing_it_Up_with_Booleans.rkt │ ├── 07_Predicates_Know_Thy_Data.rkt │ ├── ex001.rkt │ ├── ex002.rkt │ ├── ex003.rkt │ ├── ex004.rkt │ ├── ex005.rkt │ ├── ex006.rkt │ ├── ex007.rkt │ ├── ex008.rkt │ ├── ex009.rkt │ ├── ex010.rkt │ ├── images │ │ └── cat.png │ └── readme.txt ├── 02-Functions │ ├── 01_Functions.rkt │ ├── 02_Computing.rkt │ ├── 03_Composting_Functions.rkt │ ├── 04_Global_Constants.rkt │ ├── 05_Programs.rkt │ ├── ex011.rkt │ ├── ex012.rkt │ ├── ex013.rkt │ ├── ex014.rkt │ ├── ex015.rkt │ ├── ex016.rkt │ ├── ex017.rkt │ ├── ex018.rkt │ ├── ex019.rkt │ ├── ex020.rkt │ ├── ex021.rkt │ ├── ex022.rkt │ ├── ex023.rkt │ ├── ex024.rkt │ ├── ex025.rkt │ ├── ex026.rkt │ ├── ex027.rkt │ ├── ex028.rkt │ ├── ex029.rkt │ ├── ex030.rkt │ ├── ex031.rkt │ ├── ex032.rkt │ ├── first.rkt │ ├── images │ │ └── cat.png │ └── readme.txt ├── 03-HtDP │ ├── 01_Designing_Functions.rkt │ ├── 02_Finger_Exercises_Functions.rkt │ ├── 05_On_Testing.rkt │ ├── 06_Designing_World_Programs.rkt │ ├── 07_Virtual_Pet_Worlds.rkt │ ├── Word_Program_Template.rkt │ ├── add_image.rkt │ ├── allkeys.rkt │ ├── allmouse.rkt │ ├── ex033.txt │ ├── ex034.rkt │ ├── ex035.rkt │ ├── ex036.rkt │ ├── ex037.rkt │ ├── ex038.rkt │ ├── ex039.rkt │ ├── ex040.rkt │ ├── ex041.rkt │ ├── ex042.rkt │ ├── ex043a.rkt │ ├── ex043b.rkt │ ├── ex044.rkt │ ├── ex045.rkt │ ├── ex046.rkt │ ├── ex047.rkt │ ├── images │ │ └── cat.png │ └── readme.txt ├── 04-Intervals │ ├── 01_Programming_with_Conditionals.rkt │ ├── 02_Computing_Conditionaly.rkt │ ├── 03_Enumerations.rkt │ ├── 04_Intervals.rkt │ ├── 04_Ufo.rkt │ ├── 05_Itemizations.rkt │ ├── 06_Designing_with_Itemizations.rkt │ ├── 07_Finite_State_Worlds.rkt │ ├── ex048.rkt │ ├── ex049.rkt │ ├── ex050.rkt │ ├── ex051.rkt │ ├── ex052.rkt │ ├── ex053.rkt │ ├── ex054.rkt │ ├── ex055.rkt │ ├── ex056.rkt │ ├── ex057.rkt │ ├── ex058.rkt │ ├── ex059.rkt │ ├── ex060.rkt │ ├── ex061.rkt │ ├── ex062.rkt │ ├── images │ │ └── rocket.png │ ├── readme.txt │ ├── rocket1.rkt │ ├── rocket2.rkt │ ├── rocket3.rkt │ └── rocketlaunch.rkt ├── 05-Adding-Structure │ ├── 01_From_Positions to posn_Structures.rkt │ ├── 02_Computing_with_posns.rkt │ ├── 03_Programming_with_posn.rkt │ ├── 04_Defining_Structure_Types.rkt │ ├── 05_Computing_with_Structures.rkt │ ├── 06_Programming_with_Structures.rkt │ ├── 07_Universe_of_Data.rkt │ ├── 08_Designing_with_Structures.rkt │ ├── 09_Structure_in_the_World.rkt │ ├── 10_A_Graphical_Editor.rkt │ ├── 11_More_Virtual_Pets.rkt │ ├── ex063.rkt │ ├── ex064.rkt │ ├── ex065.rkt │ ├── ex066.rkt │ ├── ex067.rkt │ ├── ex068.rkt │ ├── ex069.rkt │ ├── ex070.rkt │ ├── ex071.rkt │ ├── ex072.rkt │ ├── ex073.rkt │ ├── ex074.rkt │ ├── ex075.rkt │ ├── ex076.rkt │ ├── ex077.rkt │ ├── ex078.rkt │ ├── ex079.rkt │ ├── ex080.rkt │ ├── ex081.rkt │ ├── ex082.rkt │ ├── ex083.rkt │ ├── ex084.rkt │ ├── ex085.rkt │ ├── ex086.rkt │ ├── ex087.rkt │ ├── ex088.rkt │ ├── ex089.rkt │ ├── ex090.rkt │ ├── ex091.rkt │ ├── ex092.rkt │ ├── ex093.rkt │ ├── images │ │ ├── cat.png │ │ └── chameleon.png │ └── readme.txt ├── 06-Itemizations-Structures │ ├── 01_Designing_with_Itemizations_A.rkt │ ├── 01_Designing_with_Itemizations_B.rkt │ ├── 01_Designing_with_Itemizations_C.rkt │ ├── 02_Mixing_Up_Worlds.rkt │ ├── 03_Input_Error.rkt │ ├── 04_Checking_the_World.rkt │ ├── 05_Equality_Predicates.rkt │ ├── ex094.rkt │ ├── ex095.txt │ ├── ex096.rkt │ ├── ex097.rkt │ ├── ex098.rkt │ ├── ex099.rkt │ ├── ex100.rkt │ ├── ex106.rkt │ ├── ex107.rkt │ ├── ex108.rkt │ ├── ex109.rkt │ ├── ex114a.rkt │ ├── ex114b.rkt │ ├── ex114c.rkt │ ├── images │ │ ├── cat.png │ │ ├── chameleon.png │ │ ├── pedestrian_traffic_light_green.png │ │ └── pedestrian_traffic_light_red.png │ └── readme.txt ├── 07-Intermezzo-BSL │ └── intermezzo-BSL.rkt ├── Virualcat2.rkt └── cat.png ├── 2-Arbitrarily-Large-Data ├── 08-Lists │ ├── 08_01_creating_lists.rkt │ ├── 08_03_programming_lists.rkt │ └── 08_04_computing_lists.rkt ├── 09-Designing-Self-Referential │ ├── 09_01_designing_self_ref.rkt │ ├── 09_02_non_empty_lists.rkt │ ├── 09_03_153_lecture_hall.rkt │ ├── 09_03_natural_numbers.rkt │ ├── 09_04_russian_dolls.rkt │ ├── 09_05_156_shot_world1.rkt │ ├── 09_05_157_shot_world2.rkt │ ├── 09_05_158_shot_world3.rkt │ ├── 09_05_159_lecture_hall2.rkt │ ├── 09_05_lists_and_world.rkt │ └── 09_06_note_on_lists_sets.rkt ├── 10-More-on-Lists │ ├── 10_01_161_wages1.rkt │ ├── 10_01_162_wages2.rkt │ ├── 10_01_163_FTC.rkt │ ├── 10_01_164_dollars_to_euros.rkt │ ├── 10_01_165_subst_robot.rkt │ ├── 10_01_producing_lists.rkt │ ├── 10_02_166a_Employees3.rkt │ ├── 10_02_166b_Employees4.rkt │ ├── 10_02_structures_in_lists.rkt │ ├── 10_03_lists_in_lists.rkt │ ├── 10_04_graphical_editor_revisited.rkt │ └── ttt.txt ├── 11-Design-Composition │ ├── 11_01_the_list_function.rkt │ ├── 11_03_187_game_player1.rkt │ ├── 11_03_188_email1.rkt │ ├── 11_03_functions_that_recur.rkt │ └── 11_04_functions_that_generalize.rkt └── 12-Projects-Lists │ ├── 12_01_198_word_count.rkt │ ├── 12_01_dictionaries.rkt │ ├── 12_02_iTunes.rkt │ ├── 12_03_word_games_composition.rkt │ └── test1.rkt ├── Readme.md ├── keys.org ├── notes ├── basicsteps.txt ├── definitions.md └── design_recipe.txt └── recipies.org /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.*~ 2 | *.*# 3 | *.bak 4 | *.dat 5 | -------------------------------------------------------------------------------- /0-Prologue/rocket.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/emaphis/HtDP2e-solutions/88b59b66a2f090dfd07290f16f8686c7e87298bf/0-Prologue/rocket.png -------------------------------------------------------------------------------- /0-Prologue/rocket.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname rocket) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | (require 2htdp/image) 5 | (require 2htdp/universe) 6 | 7 | ;; rocket scent version 6 8 | ;; more realistic landing 9 | 10 | ; NOTE: I'm getting graphics from a file on the computer 11 | ; so I can leave rkt files in a text mode. 12 | (define ROCKET (bitmap "rocket.png")) 13 | 14 | ; properties of the "world" and descending rocket 15 | (define WIDTH 100) 16 | (define HEIGHT 200) 17 | (define V 3) 18 | (define X 50) 19 | 20 | ; other constants 21 | (define MTSCN (empty-scene WIDTH HEIGHT "light blue")) 22 | (define ROCKET-CENTER-TO-BOTTOM 23 | (- HEIGHT (/ (image-height ROCKET) 2))) 24 | 25 | ; functions 26 | (define (picture-of-rocket t) 27 | (cond 28 | [(<= (distance t) ROCKET-CENTER-TO-BOTTOM) 29 | (place-image ROCKET X (distance t) MTSCN)] 30 | [(> (distance t) ROCKET-CENTER-TO-BOTTOM) 31 | (place-image ROCKET X ROCKET-CENTER-TO-BOTTOM MTSCN)])) 32 | 33 | (define (distance t) 34 | (* V t)) 35 | 36 | (animate picture-of-rocket) 37 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/01-Arithmetic/01_The_Arithmetic_of_Numbers.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 01_The_Arithmetic_of_Numbers) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 1.1 The Arithmetic of Numbers 5 | 6 | (+ 3 4) 7 | 8 | ;; BSL provides operators: +, -, *, /, abs, add1, ceiling, denominator, exact->inexact, expt, floor, 9 | ;; gcd, log, max, numerator, quotient, random, remainder, sqr, and tan 10 | 11 | (sin 0) 12 | 13 | ;; Types of numbers include counting numbers, integers, rational numbers, real numbers, 14 | ;; and even complex numbers. 15 | 16 | ;; See exercise 1 -------------------------------------------------------------------------------- /1-Fixed-Size-Data/01-Arithmetic/02_The_Arithmetic_of_Strings.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 02_The_Arithmetic_of_Strings) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 1.2 The Arithmetic of Strings 5 | 6 | ;; examples 7 | "hello" 8 | "world" 9 | "blue" 10 | "red" 11 | 12 | ;; 1Strings: - lenth of one 13 | "r" 14 | "e" 15 | "d" 16 | 17 | ;; string-append consumes and produces strings 18 | (string-append "what a " "lovely " "day" " 4 BSL") 19 | ;;=> "what a lovely day 4 BSL" 20 | ;; string-append is analogous to '+' for numbers. 21 | 22 | ;; See exercise 2. -------------------------------------------------------------------------------- /1-Fixed-Size-Data/01-Arithmetic/03_Mixing_It_Up.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 03_Mixing_It_Up) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 1.3 Mixing It Up 5 | ;; other string operationgs consume or produce data other than strings 6 | ;; string-length - consumes a String and prduces a Number 7 | ;; string-ith - consumes a String and a Natural and produces a !String 8 | ;; number->string - consumes a number and produces a string 9 | 10 | ;; substring 11 | ;; (substring s i j) → string 12 | ;; s : string 13 | ;; i : natural-number 14 | ;; j : natural-number 15 | ;; Extracts the substring starting at i up to j (or the end if j is not provided). 16 | 17 | ;; Inappropiate arguments 18 | ;(string-length 42 19 | ;string-length: expects a string, given 42) 20 | 21 | (string-length "fourty-two") ;=> 10 22 | ;(string-length 42) type error 23 | 24 | ;; we may nest operations if we keep track of types: 25 | (+ (string-length "hello world") 20) ;=> 31 26 | 27 | 28 | (+ (string-length (number->string 42)) 2) ;=> 4 29 | ;;(+ (string-length 42) 2) type error 30 | ;; string-length: expects a string, given 42 31 | 32 | ;; See exercises 3, 4. -------------------------------------------------------------------------------- /1-Fixed-Size-Data/01-Arithmetic/04_Mixing_It_Up.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 04_Mixing_It_Up) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/01-Arithmetic/04_The_Arithmetic_of_Images.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 04_The_Arithmetic_of_Images) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | (require 2htdp/image) 5 | 6 | ;; 1.4 Arithmetic of Images 7 | 8 | ;; creation of basic images given size, mode, color: 9 | ;; circle ellipse line rectangle square text triangle 10 | ;; mode: "solid" "outline" 11 | ;; color: name passed as string 12 | 13 | (circle 10 "solid" "green") 14 | 15 | (rectangle 10 20 "solid" "blue") 16 | 17 | (star 12 "solid" "gray") 18 | 19 | ;; Stop! 20 | (text "Hello, World!" 36 "purple") 21 | 22 | ;; functions returning image properties: 23 | ;; image-width, image-height 24 | 25 | (image-width (circle 10 "solid" "red")) 26 | 27 | (image-height (rectangle 10 20 "solid" "blue")) 28 | 29 | ;; Stop! 30 | (+ (image-width (circle 10 "solid" "red")) 31 | (image-height (rectangle 10 20 "solid" "blue"))) 32 | ;; it evaluates each expressin in the parentheses working 33 | ;; inside out. 34 | 35 | ;; functions that compose images: 36 | ;; overlay, overlay/xy, overlay/align 37 | 38 | ;; overlay/align x-place y-place i1 i2 is 39 | ;; x-place: left right middle center pinhole 40 | ;; y-place: top bottom, middle center baseline pinhole: 41 | 42 | ;; scene fuctions: 43 | ;; place-image, empty-scene, add-line 44 | 45 | ;; See exercises 5, 6 -------------------------------------------------------------------------------- /1-Fixed-Size-Data/01-Arithmetic/05_The_Arithmetic_of_Booleans.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 05_The_Arithmetic_of_Booleans) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; 1.5 The Arithmetic of Booleans 5 | 6 | (or #true #true) ; #true 7 | (or #true #false) ; #true 8 | (or #false #true) ; #true 9 | (or #false #false) ; #false 10 | 11 | (and #true #true) ; #true 12 | (and #true #false) ; #false 13 | (and #false #true) ; #false 14 | (and #false #false) ; #false 15 | 16 | (not #true) ; #false 17 | 18 | ;; Se exercise 7 -------------------------------------------------------------------------------- /1-Fixed-Size-Data/01-Arithmetic/06_Mixing_it_Up_with_Booleans.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 06_Mixing_it_Up_with_Booleans) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;;; 1.6 Mixing It Up with Booleans 5 | 6 | ;; definitions 7 | (define x 2) 8 | 9 | ;; compute inverse of x 10 | (define inverse-of-x (/ 1 x)) 11 | 12 | ;; but what if x is zero? 13 | ;; use an if expression as a guard expression 14 | (if (= x 0) 0 (/ 1 x)) 15 | 16 | ;Stop! 17 | (define x1 0) 18 | (if (= x1 0) 0 (/ 1 x1)) ; should be 0 19 | 20 | ;; boolean expressions for strings 21 | ;; string=? string>=? string<=? 22 | 23 | ;; See exercise 8 24 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/01-Arithmetic/07_Predicates_Know_Thy_Data.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 07_Predicates_Know_Thy_Data) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ; 1.7 Predicates: Know Thy Data 5 | 6 | ;; string-length: expects a string, given 42 7 | ;;(* (+ (string-length 42) 1) pi) 8 | 9 | (number? 4) ; #true 10 | (number? pi) ; #true 11 | (number? #true) ; #false 12 | (number? "fortytwo") ; #false 13 | 14 | (define in 42) 15 | 16 | (if (string? in) 17 | (string-length in) 18 | 0) 19 | 20 | ;; Type predicates: 21 | ;; number? 22 | ;; string? image? boolean? 23 | ;; interger? rational? real? complex? 24 | ;; exact? Inexact? 25 | 26 | ;; See exercises 9, 10 -------------------------------------------------------------------------------- /1-Fixed-Size-Data/01-Arithmetic/ex001.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex001) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex 1 5 | ;; The primary goal of this exercise is to create an expression that compute 6 | ;; s the distance of some specific Cartesian point (x,y) from the origin (0,0). 7 | ; ;The secondary goal is to get some first exposure to BSL, DrRacket and its 8 | ;; interactions area to develop expressions. 9 | 10 | (define x 3) 11 | (define y 4) 12 | 13 | (sqrt (+ (sqr x) (sqr y))) ;=> 5 14 | (sqrt (+ (sqr 12) (sqr 5))) ;=> 13 -------------------------------------------------------------------------------- /1-Fixed-Size-Data/01-Arithmetic/ex002.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex002) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 2 5 | ;; Add the following two lines to the definitions area: 6 | ;; (define prefix "hello") 7 | ;; (define suffix "world") 8 | ;; Then use string primitives to create an expression 9 | ;; that concatenates prefix and suffix and adds "_" between them. 10 | ;; When you run this program, you will see "hello_world" in the interactions area. 11 | 12 | (define prefix "hello") 13 | (define suffix "world") 14 | (string-append prefix "_" suffix) ;=> "hello_world" 15 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/01-Arithmetic/ex003.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex003) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 3 5 | ;; Add the following two lines to the definitions area: 6 | ;; (define str "helloworld") 7 | ;; (define i 5) 8 | ;; Then create an expression using string primitives that adds "_" at position i. 9 | ;; In general this means the resulting string is longer than the original one; 10 | ;; here the expected result is "hello_world". 11 | 12 | (define str "helloworld") 13 | (define i 5) 14 | 15 | (string-append (substring str 0 i) "_" (substring str i)) 16 | ;=> "hello_world" -------------------------------------------------------------------------------- /1-Fixed-Size-Data/01-Arithmetic/ex004.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex004) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 4 5 | ;; Use the same setup as in exercise 3 to create an expression that deletes the ith position from str. 6 | ;; Clearly this expression creates a shorter string than the given one. 7 | ;; Which values for i are legitimate? 8 | 9 | (define str "helloworld") 10 | (define i 5) 11 | 12 | ;; delete i'th character 13 | (string-append (substring str 0 i) (substring str (+ i 1))) 14 | ;=>"helloorld" 15 | 16 | ;; range 0 to 9 will work 17 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/01-Arithmetic/ex005.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex005) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | (require 2htdp/image) 5 | 6 | ;; Ex. 5 7 | ;; Use the picture primitives to create the image of a simple 8 | ;; a car! 9 | (overlay/xy 10 | (rectangle 12 5 "solid" "lightblue") 11 | -6 5 12 | (overlay/xy 13 | (rectangle 25 7 "solid" "red") 14 | 0 3 15 | (overlay/xy 16 | (circle 4 "solid" "black") 17 | 17 00 18 | (circle 4 "solid" "black")))) -------------------------------------------------------------------------------- /1-Fixed-Size-Data/01-Arithmetic/ex006.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex006) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | (require 2htdp/image) 5 | 6 | ;; Ex. 6 -- cat image 7 | ;; Create an expression that counts the number of pixels in the image. 8 | 9 | (define cat (bitmap "images/cat.png")) 10 | 11 | (image-height cat) 12 | (image-width cat) 13 | 14 | ;; expression -- (* 117 75) 15 | (* (image-height cat) (image-width cat)) ;=> 8775 16 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/01-Arithmetic/ex007.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex007) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 7 5 | ;; 6 | ;; Add these two lines to the definitions area of DrRacket: 7 | ;; (define sunny #true) 8 | ;; (define friday #false) 9 | ;; Now create an expression that computes whether sunny is false or friday is true. 10 | ;; So in this particular case, the answer is #false. (Why?) 11 | ;; How many combinations of Booleans can you associate with sunny and friday? 12 | 13 | (define sunny #true) 14 | (define friday #false) 15 | 16 | (or (not sunny) friday) ; #false 17 | 18 | ;; logical implication! 19 | (or (not #true) #true) ; #true 20 | (or (not #true) #false) ; #false 21 | (or (not #false) #true) ; #true 22 | (or (not #false) #false) ; #true 23 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/01-Arithmetic/ex008.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex008) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | (require 2htdp/image) 5 | 6 | ;; Ex. 8 7 | ;; Add the following line to the definitions area: 8 | ;; (define cat ) 9 | ;; Create a conditional expression that computes whether the image is tall or wide. 10 | ;; An image should be labeled "tall" if its height is larger than or equal to its width; 11 | ;; otherwise it is "wide". 12 | ;; Replace the cat with a rectangle of your choice to ensure that you know the expected answer. 13 | ;; Now try the following modification. 14 | ;; Create an expression that computes whether a picture is "tall", "wide", or "square" 15 | 16 | (define cat (bitmap "images/cat.png")) 17 | 18 | (if (> (image-height cat) (image-width cat)) 19 | "tall" 20 | (if (= (image-height cat) (image-width cat)) 21 | "square" 22 | "wide")) ;=> "tall" 23 | 24 | (define image-1 (rectangle 60 40 "solid" "red")) 25 | ;;(rectangle 60 40 "solid" "red") 26 | 27 | (if (> (image-height image-1) (image-width image-1)) 28 | "tall" 29 | (if (= (image-height image-1) (image-width image-1)) 30 | "square" 31 | "wide")) ;=> "tall" -------------------------------------------------------------------------------- /1-Fixed-Size-Data/01-Arithmetic/ex009.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex009) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | (require 2htdp/image) 5 | 6 | ;; Ex. 9 7 | ;; Add the following line to the definitions area of DrRacket: 8 | ;; (define in ...) 9 | ;; Then create an expression that converts the value of in to a positive number. 10 | ;; For a String, it determines how long the String is; 11 | ;; for an Image, it uses the area; 12 | ;; for a Number, it decrements the number by 1, unless it is already 0 or negative; 13 | ;; for #true it uses 10 and for #false 20. 14 | 15 | ;;(define in "hello") 16 | ;;(define in (square 10 "outline" "green")) 17 | ;;(define in 100) 18 | (define in #false) 19 | 20 | (if (string? in) 21 | (string-length in) 22 | (if (image? in) 23 | (* (image-height in) (image-width in)) 24 | (if (number? in) 25 | (if (> in 0) 26 | (- in 1) 27 | (abs in)) 28 | (if (boolean? in) 29 | (if in 30 | 10 31 | 20) 32 | (error "Unknown data type"))))) 33 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/01-Arithmetic/ex010.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex010) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 10 5 | ;; Now relax, eat, sleep, and then tackle the next chapter. 6 | 7 | ;; Ok!! 8 | "zzz...." 9 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/01-Arithmetic/images/cat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/emaphis/HtDP2e-solutions/88b59b66a2f090dfd07290f16f8686c7e87298bf/1-Fixed-Size-Data/01-Arithmetic/images/cat.png -------------------------------------------------------------------------------- /1-Fixed-Size-Data/01-Arithmetic/readme.txt: -------------------------------------------------------------------------------- 1 | ;; 1.1 Arithmatic 2 | 3 | #| 4 | Every language comes with a language of defining data and a language 5 | of operations on that data. Atomic data is used to repesent real-wrold 6 | data in the lanuage. Programmers must learn to compose atomic data into 7 | compound data to model the world. Programmers also compose those 8 | operations on atomic data into programs. Combinations of these two 9 | parts of the language fallow the rules of arithetic. 10 | |# 11 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/01_Functions.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 01_Functions) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | (require 2htdp/image) 5 | 6 | ;; 2.1 funcions 7 | 8 | ;; Definitions examples 9 | 10 | ;; Silly functions that don't use the parameters 11 | (define (f x) 1) 12 | (define (g x y) (+ 1 1)) 13 | (define (h x y z) (+ (* 2 2) 3)) 14 | 15 | ;; doesn't matter what parameters ypu pass 16 | (f 1) ; 1 17 | (f "hello world") ; 1 18 | (f #true) ;1 19 | (f (circle 3 "solid" "red")) 20 | (g 1 2) ; 2 21 | (g 1 "hello") ; 2 22 | (h 2 "hello" #false) ; 7 23 | 24 | 25 | ;; a useful funtion 26 | (define (ff a) 27 | (* 10 a)) 28 | 29 | ;; aplications (fn par1 par2 par3) 30 | (ff 2) ; 20 31 | (ff 4) ; 40 32 | 33 | ;; See exercises 11 - 20 34 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/02_Computing.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 02_Computing) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e 2.2 Functions 5 | ;; 2.2 Computing 6 | ;; Exercises 21-26 7 | 8 | (define (f x) 1) 9 | 10 | ;; evaluatets 11 | (f (+ 1 1)) 12 | ;(f 2) 13 | ;1 14 | 15 | 16 | (define (ff a) 17 | (* 10 a)) 18 | 19 | ;; evaluate 20 | (ff (+ 1 1)) 21 | ;(ff 2) 22 | ;(* 10 2) 23 | ;20 24 | 25 | (+ (ff (+ 1 2)) 2) 26 | ;(+ (ff 3) 2) 27 | ;(+ (* 10 3) 2) 28 | ;(+ 30 2) 29 | ;s32 30 | 31 | 32 | ;; See exercise 21 33 | 34 | ;;; programs that process strings 35 | 36 | (string-append "hello" " " "world") ; "hello world" 37 | (string-append "bye" ", " "world") ; "bye, world" 38 | 39 | 40 | ;; Sample program 41 | ;; Attendee's example and Letter example 42 | 43 | (define (opening first-name last-name) 44 | (string-append "Dear " first-name ",")) 45 | 46 | (opening "Matthew" "Fisler") ; "Dear Matthew," 47 | 48 | ;; See exercises 22, 23, 24, 25, 26 49 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/04_Global_Constants.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 04_Global_Constants) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e 2.3 Functions 5 | ;; 2.4 Global Constants 6 | ;; Exercise 30 7 | 8 | (require 2htdp/image) 9 | 10 | ;; Examples 11 | 12 | ; the current price of a movie ticket 13 | (define CURRENT-PRICE 5) 14 | 15 | ; useful to compute the area of a disk: 16 | (define ALMOST-PI 3.14) 17 | 18 | ; a blank line: 19 | (define NL "\n") 20 | 21 | ; an empty scene: 22 | (define MT (empty-scene 100 100)) 23 | 24 | ; literal constants 25 | (define WIDTH 100) 26 | (define HEIGHT 200) 27 | 28 | ; arbitrary expressions 29 | (define MID-WIDTH (/ WIDTH 2)) 30 | (define MID-HEIGHT (/ HEIGHT 2)) 31 | 32 | ;; see exercise 30. 33 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/05_Programs.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 05_Programs) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e 2.2 Functions 5 | ;; 2.5 Programs 6 | ;; Ex 33,34 7 | 8 | ;; We are now ready to create simple programs. 9 | 10 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 11 | ;; Batch Programs 12 | 13 | ;; reading and writing batch data to and from disk 14 | (require 2htdp/batch-io) 15 | 16 | ;>(write-file "sample.dat" "212"); 17 | ;"sample.dat" 18 | 19 | ;>(read-file "sample.dat") 20 | ;"212" 21 | 22 | ;; You can read and write to standard input and output 23 | 24 | ;> (write-file 'stdout "212\n") 25 | ;212 26 | ;'stdout 27 | 28 | 29 | ;;; Example batch program that calulates temperatures 30 | 31 | ;; Formulat for calculating Celsius from Fahrenheit 32 | (define (C f) 33 | (* 5/9 (- f 32))) 34 | 35 | (C 32) ; 0 36 | (C 212) ; 100 37 | (C -40) ; -40 38 | 39 | ;; main program 40 | (define (convert in out) 41 | (write-file out 42 | (string-append 43 | (number->string 44 | (C 45 | (string->number 46 | (read-file in)))) 47 | "\n"))) 48 | 49 | ;> (write-file "sample.dat" "212") 50 | ;"sample.dat" 51 | 52 | ;> (convert "sample.dat" 'stdout) 53 | ;100 54 | ;'stdout 55 | 56 | ;> (convert "sample.dat" "out.dat") 57 | ;"out.dat" 58 | 59 | ;> (read-file "out.dat") 60 | ;"100" 61 | 62 | 63 | ;; See exercise 31 64 | 65 | 66 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 67 | ;; Interactive Programs 68 | 69 | ;; See Exercise 42 70 | 71 | 72 | ;;; Universe and big-bang 73 | 74 | (require 2htdp/image) 75 | (require 2htdp/universe) 76 | 77 | 78 | ;; first basic definitions: 79 | (define (number->square s) 80 | (square s "solid" "red")) 81 | 82 | ; >(number->square 5) 83 | 84 | ; > (big-bang 100 [to-draw number->square]) 85 | 86 | 87 | ;; more interesting big-bang expession 88 | #; 89 | (big-bang 100 90 | [to-draw number->square] 91 | [on-tick sub1] 92 | [stop-when zero?]) 93 | 94 | 95 | ;; a function that consumes the current state and a string that describes 96 | ;; the key event and then returns a new state: 97 | (define (reset s ke) 98 | 100) 99 | 100 | #; ; now handle keypresses 101 | (big-bang 100 102 | [to-draw number->square] 103 | [on-tick sub1] 104 | [stop-when zero?] 105 | [on-key reset]) 106 | 107 | #; schematic big-bang 108 | (big-bang cw0 109 | [on-tick tock] 110 | [on-key ke-h] 111 | [on-mouse me-h] 112 | [to-draw render] 113 | [stop-when end?] 114 | ...) 115 | 116 | 117 | ;; See "first.rkt" for first interactive program from figure 18:- 118 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/ex011.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex011) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 11: 5 | ;; Define a function that consumes two numbers, x and y, and that 6 | ;; computes the distance of point (x,y) to the origin. 7 | ;; based on ex. 1 8 | 9 | (define (distance x y) 10 | (sqrt (+ (sqr x) (sqr y)))) 11 | 12 | (distance 0 0) ;; 0 13 | (distance 3 4) ;; 5 14 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/ex012.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex012) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 12: 5 | ;; Define the function cvolume, which accepts the length of a side of 6 | ;; an equilateral cube and computes its volume. If you have time, consider 7 | ;; defining csurface, too. 8 | 9 | (define (cvolume side) 10 | (* side side side)) 11 | 12 | (cvolume 0) ; 0 13 | (cvolume 1) ; 1 14 | (cvolume 5) ; 125 15 | 16 | 17 | (define (csurface side) 18 | (* (sqr side) 6)) 19 | 20 | (csurface 0) ; 0 21 | (csurface 1) ; 6 22 | (csurface 5) ; 150 23 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/ex013.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex013) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 13: 5 | ;; Define the function string-first, which extracts the first 1String from a 6 | ;; non-empty string. Don’t worry about empty strings. 7 | 8 | (define (string-first str) 9 | (substring str 0 1)) 10 | 11 | (string-first "s") ; "s" 12 | (string-first "string") ; "s" 13 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/ex014.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex014) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 14: 5 | ;; Define the function string-last, which extracts the last 1String from 6 | ;; a non-empty string. Don’t worry about empty strings. 7 | 8 | (define (string-last str) 9 | (substring str (- (string-length str) 1))) 10 | 11 | (string-last "d") ; "d" 12 | (string-last "hello world") ; "d" 13 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/ex015.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex015) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 15 5 | ;; Define ==>. The function consumes two Boolean values, call them sunny 6 | ;; and friday. Its answer is #true if sunny is false or friday is true. 7 | ;; Note: Logicians call this Boolean operation implication, and they use the 8 | ;; notation sunny => friday for this purpose. image 9 | ;; Based on ex 7 -- bool imply function 10 | 11 | (define (==> p q) 12 | (or (not p) q)) 13 | 14 | ;; imply truth table 15 | (==> #true #true) ; #true 16 | (==> #true #false) ; #false 17 | (==> #false #true) ; #true 18 | (==> #false #false) ; #true 19 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/ex016.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex016) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | (require 2htdp/image) 5 | 6 | ;; Ex. 16: 7 | ;; Define the function image-area, which counts the number of pixels in a given image. 8 | ;; based on ex. 5 9 | 10 | (define (image-area img) 11 | (* (image-height img) (image-width img))) 12 | 13 | (image-area (rectangle 0 0 "solid" "blue")) ; 0 14 | (image-area (rectangle 10 20 "solid" "red")) ; 200 15 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/ex017.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex017) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | (require 2htdp/image) 5 | 6 | ;; Ex. 17: 7 | ;; Define the function image-classify, which consumes an image and produces 8 | ;; "tall" if the image is taller than wide, "wide" if it is wider than tall, 9 | ;; or "square" if its width and height are the same. 10 | ;; based on ex. 8 11 | 12 | (define (image-classify img) 13 | (if (> (image-height img) (image-width img)) 14 | "tall" 15 | (if (= (image-height img) (image-width img)) 16 | "square" 17 | "wide"))) 18 | 19 | (define TALL (rectangle 10 20 "solid" "blue")) 20 | (define WIDE (rectangle 20 10 "solid" "blue")) 21 | (define SQUARE (rectangle 20 20 "solid" "blue")) 22 | 23 | (image-classify TALL) ; "tall" 24 | (image-classify WIDE) ; "wide" 25 | (image-classify SQUARE) ; "square" 26 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/ex018.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex018) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 18 5 | ;; Define the function string-join, which consumes two strings and 6 | ;; appends them with "_" in between. 7 | ;; based on ex. 2 8 | 9 | (define (string-join prefix suffix) 10 | (string-append prefix "_" suffix)) 11 | 12 | (string-join "" "") ; "_" 13 | (string-join "" "world") ; "_world 14 | (string-join "hello" "world") ; "hello_world" -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/ex019.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex019) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Exercise 19 5 | ;; Define the function string-insert, which consumes a string str plus 6 | ;; a number i and inserts "_" at the ith position of str. Assume i is 7 | ;; a number between 0 and the length of the given string (inclusive). 8 | ;; based on ex. 3 9 | 10 | (define (string-insert str i) 11 | (string-append (substring str 0 i) "_" (substring str i))) 12 | 13 | (define str "helloworld") 14 | (define i 5) 15 | 16 | (string-insert "" 0) ; "_" 17 | (string-insert "helloworld" 5) ; "hello_world" 18 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/ex020.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex020) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex 20 5 | ;; Define the function string-delete, which consumes a string plus a number 6 | ;; i and deletes the ith position from str. Assume i is a number between 0 7 | ;; (inclusive) and the length of the given string (exclusive). 8 | ;; Can string-delete deal with empty strings? 9 | ;; based on ex. 4 10 | 11 | (define (string-delete str i) 12 | (string-append (substring str 0 i) (substring str (+ i 1)))) 13 | 14 | (string-delete "helloworld" 5) ; "helloorld" 15 | 16 | ;; Cant handle 0 length strings -- starting index out of range 17 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/ex021.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex021) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 21: 5 | ;; Use DrRacket’s stepper to evaluate (ff (ff 1)) step by step. 6 | ;; Also try (+ (ff 1) (ff 1)). 7 | ;; Does DrRacket’s stepper reuse the results of computations? 8 | 9 | (define (ff a) 10 | (* 10 a)) 11 | 12 | (ff (ff 1)) 13 | (ff (* 10 1)) 14 | (ff 10) 15 | (* 10 10) 16 | 100 17 | 18 | (+ (ff 1) (ff 1)) 19 | (+ (* 10 1) (ff 1)) 20 | (+ 10 (ff 1)) 21 | (+ 10 (* 10 1)) 22 | (+ 10 10) 23 | 20 24 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/ex022.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex022) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 22 5 | ;; Use DrRacket’s stepper on this program fragment: 6 | ;; (define (distance-to-origin x y) 7 | ;; (+ (sqr x) (sqr y)))) 8 | ;; (distance-to-origin 3 4) 9 | ;; Does the explanation match your intuition? 10 | 11 | (define (distance-to-origin x y) 12 | (sqrt (+ (sqr x) (sqr y)))) 13 | 14 | (distance-to-origin 3 4) 15 | (sqrt (+ (sqr 3) (sqr 4))) 16 | (sqrt (+ 9 (sqr 4))) 17 | (sqrt (+ 9 16)) 18 | (sqrt 25) 19 | 5 20 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/ex023.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex023) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 23: 5 | ;; The first 1String in "hello world" is "h". 6 | ;; How does the following function compute this result? 7 | ;; (define (string-first s) 8 | ;; (substring s 0 1)) 9 | ;; Use the stepper to confirm your ideas. 10 | 11 | 12 | (define (string-first s) 13 | (substring s 0 1)) 14 | 15 | (string-first "hello world") 16 | (substring "hello world" 0 1) 17 | "h" 18 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/ex024.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex024) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex 24: run in stepper 5 | ;; Here is the definition of ==> 6 | ;; (define (==> x y) 7 | ;; (or (not x) y)) 8 | ;; Use the stepper to determine the value of (==> #true #false). 9 | 10 | (define (==> x y) 11 | (or (not x) y)) 12 | 13 | (==> #true #false) 14 | (or (not #true) #false) 15 | (or #false #false) 16 | #false 17 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/ex025.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex025) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex 25 5 | 6 | (require 2htdp/image) 7 | 8 | ;;Take a look at this attempt to solve exercise 17: 9 | 10 | (define (image-classify img) 11 | (cond 12 | [(>= (image-height img) (image-width img)) 13 | "tall"] 14 | [(= (image-height img) (image-width img)) 15 | "square"] 16 | [(<= (image-height img) (image-width img)) 17 | "wide"])) 18 | 19 | (image-classify (circle 3 "solid" "red")) 20 | 21 | ;; Does stepping through an application suggest a fix? 22 | ;; => Yes the "=" clause should come first. 23 | 24 | ;; fixed 25 | (define (image-classify-fixed img) 26 | (cond 27 | [(= (image-height img) (image-width img)) 28 | "square"] 29 | [(>= (image-height img) (image-width img)) 30 | "tall"] 31 | [(<= (image-height img) (image-width img)) 32 | "wide"])) 33 | 34 | (image-classify-fixed (circle 3 "solid" "red")) -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/ex026.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex026) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex 26: 5 | ;; What do you expect as the value of this program: 6 | 7 | (define (string-insert s i) 8 | (string-append (substring s 0 i) 9 | "_" 10 | (substring s i))) 11 | 12 | ;; Confirm your expectation with DrRacket and its stepper. 13 | 14 | (string-insert "helloworld" 6) 15 | (string-append (substring "helloworld" 0 6) "_" (substring "helloworld" 6)) 16 | (string-append "hellow" "_" (substring "helloworld" 6)) 17 | (string-append "hellow" "-" (substring "helloworld" 6)) 18 | (string-append "hellow" "-" "orld") 19 | "hellow_orld" 20 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/ex027.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex027) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; definitions 5 | (define BASE-ATTENDEES 120) 6 | (define BASE-PRICE 5.00) 7 | 8 | (define ATTENDEE-CHANGE 15) 9 | (define PRICE-CHANGE 0.10) 10 | 11 | (define FIXED-COST 180.00) 12 | (define ATTENDEE-COST 0.04) 13 | 14 | ;; functions 15 | (define (attendees ticket-price) 16 | (+ BASE-ATTENDEES 17 | (* (/ ATTENDEE-CHANGE PRICE-CHANGE) 18 | (- BASE-PRICE ticket-price)))) 19 | 20 | (define (revenue ticket-price) 21 | (* ticket-price (attendees ticket-price))) 22 | 23 | (define (cost ticket-price) 24 | (+ FIXED-COST 25 | (* ATTENDEE-COST (attendees ticket-price)))) 26 | 27 | (define (profit ticket-price) 28 | (- (revenue ticket-price) 29 | (cost ticket-price))) 30 | 31 | ;; examples 32 | (profit 1.00) ; 511.2 33 | (profit 2.00) ; 937.2 34 | (profit 2.90) ; 1064.1 35 | (profit 3.00) ; 1063.2 36 | (profit 4.00) ; 889.2 37 | (profit 5.00) ; 415.2 38 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/ex028.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex028) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ; Ex. 28: 5 | ;; Determine the potential profit for these ticket prices: $1, $2, $3, $4, and $5. 6 | ;; Which price should the owner of the movie theater choose to maximize his profits? 7 | ;; Determine the best ticket price to a dime. 8 | ;; monolith version 9 | 10 | (define (profit price) 11 | (- (* (+ 120 12 | (* (/ 15 0.1) 13 | (- 5.0 price))) 14 | price) 15 | (+ 180 16 | (* 0.04 17 | (+ 120 18 | (* (/ 15 0.1) 19 | (- 5.0 price))))))) 20 | 21 | ;; examples 22 | (profit 2.00) ; 937.2 23 | (profit 2.90) ; 1064.1 24 | (profit 3.00) ; 1063.2 25 | (profit 4.00) ; 889.2 26 | 27 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/ex029.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex029) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; ex 29. 5 | 6 | ;; After studying the costs of a show, the owner discovered several ways of lowering the cost. 7 | ;; As a result of these improvements, there is no longer a fixed cost; 8 | ;; a variable cost of $1.50 per attendee remains. 9 | ;; Modify both programs to reflect this change. 10 | ;; When the programs are modified, 11 | ;; test them again with ticket prices of $3, $4, and $5 and compare the results. 12 | 13 | ;; definitions 14 | (define AVG-ATTENDEES 120) 15 | (define BASE-PRICE 5.00) 16 | 17 | (define ATTENDANCE-CHANGE 15) 18 | (define PRICE-CHANGE 0.10) 19 | 20 | (define VARIABLE-COST 1.50) 21 | 22 | ;; functions 23 | (define (attendees ticket-price) 24 | (- AVG-ATTENDEES 25 | (* (- ticket-price BASE-PRICE) 26 | (/ ATTENDANCE-CHANGE PRICE-CHANGE)))) 27 | 28 | (define (revenue ticket-price) 29 | (* ticket-price (attendees ticket-price))) 30 | 31 | (define (cost ticket-price) 32 | (* VARIABLE-COST (attendees ticket-price))) 33 | 34 | (define (profit ticket-price) 35 | (- (revenue ticket-price) 36 | (cost ticket-price))) 37 | 38 | ;; examples 39 | (profit 1.00) ; -360 40 | (profit 2.00) ; 285 41 | (profit 3.00) ; 630 42 | (profit 4.00) ; 675 43 | (profit 5.00) ; 420 44 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/ex030.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex030) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 30: 5 | ;; Define constants for the price optimization program so that the price 6 | ;; sensitivity of attendance (15 people for every 10 cents) becomes a computed constant. 7 | 8 | ;; definitions 9 | (define ATTENDEES 15) 10 | (define PRICE 0.10) 11 | (define PRICE-SENSITIVITY (/ ATTENDEES PRICE)) ; 150 12 | 13 | (define BASE-ATTENDEES 120) 14 | (define BASE-PRICE 5.00) 15 | 16 | (define ATTENDANCE-CHANGE 15) 17 | (define PRICE-CHANGE 0.10) 18 | 19 | (define VARIABLE-COST 1.50) 20 | 21 | ;; functions 22 | (define (attendees ticket-price) 23 | (- BASE-ATTENDEES 24 | (* (- ticket-price BASE-PRICE) PRICE-SENSITIVITY))) 25 | 26 | (define (revenue ticket-price) 27 | (* ticket-price (attendees ticket-price))) 28 | 29 | (define (cost ticket-price) 30 | (* VARIABLE-COST (attendees ticket-price))) 31 | 32 | (define (profit ticket-price) 33 | (- (revenue ticket-price) 34 | (cost ticket-price))) 35 | 36 | ;; examples 37 | (profit 2.00) ; 285 38 | (profit 3.00) ; 630 39 | (profit 4.00) ; 675 40 | (profit 5.00) ; 420 41 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/ex031.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex031) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 31: 5 | ;; Recall the letter program from Composing Functions. 6 | ;; Here is how to launch the program and have it write its output to the 7 | ;; interactions area: 8 | 9 | (require 2htdp/batch-io) 10 | 11 | ;; Letter sample -- function composition 12 | ;; defined last section 13 | 14 | (define (letter fst lst signature-name) 15 | (string-append 16 | (opening fst) 17 | "\n\n" 18 | (body fst lst) 19 | "\n\n" 20 | (closing signature-name))) 21 | 22 | (define (opening fst) 23 | (string-append "Dear " fst ",")) 24 | 25 | (define (body fst lst) 26 | (string-append 27 | "We have discovered that all people with the" "\n" 28 | "last name " lst " have won our lottery. So, " "\n" 29 | fst ", " "hurry and pick up your prize.")) 30 | 31 | (define (closing signature-name) 32 | (string-append 33 | "Sincerely," 34 | "\n\n" 35 | signature-name 36 | "\n")) 37 | 38 | ;; Here is a letter-writing batch program that reads names from three files 39 | ;; and writes a letter to one: 40 | (define (main in-fst in-lst in-signature out) 41 | (write-file out 42 | (letter (read-file in-fst) 43 | (read-file in-lst) 44 | (read-file in-signature)))) 45 | 46 | 47 | ;; Exercise functions: 48 | 49 | ;(write-file "first.dat" "Matt") 50 | ;(write-file "last.dat" "Johnson") 51 | ;(write-file "sig.dat" "Edward") 52 | 53 | ;(main "first.dat" "last.dat" "sig.dat" "letter.dat") 54 | 55 | ;(read-file "letter.dat") 56 | 57 | ;(letter "Robby" "Flatt" "Felleisen") 58 | ;(letter "Christopher" "Columbus" "Felleisen") 59 | ;(letter "ZC" "Krishnamurthi" "Felleisen") 60 | 61 | ;(write-file "Matthew-Krishnamurthi.txt" 62 | ; (letter "Matthew" "Krishnamurthi" "Felleisen")) 63 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/ex032.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex032) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex 32: 5 | 6 | ;; Ten different events varius interactive programs will have to deal with: 7 | ;; Eye movements, skin temperature, heart rate, screan touches, car speeds, car postions, 8 | ;; incomming messages. 9 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/first.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname first) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; first interactive program from fig 18 "interactive programs" 5 | 6 | (require 2htdp/image) 7 | (require 2htdp/universe) 8 | 9 | (define BACKGROUND (empty-scene 100 100)) 10 | (define DOT (circle 3 "solid" "red")) 11 | 12 | (define (main y) 13 | (big-bang y 14 | [on-tick sub1] 15 | [stop-when zero?] 16 | [to-draw place-dot-at] 17 | [on-key stop])) 18 | 19 | (define (place-dot-at y) 20 | {place-image DOT 50 y BACKGROUND}) 21 | 22 | (define (stop y ke) 23 | 0) 24 | 25 | ;; Try out program 26 | ; > (place-dot-at 89) 27 | ; > (stop 89 "q") 28 | ; > (main 90) 29 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/images/cat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/emaphis/HtDP2e-solutions/88b59b66a2f090dfd07290f16f8686c7e87298bf/1-Fixed-Size-Data/02-Functions/images/cat.png -------------------------------------------------------------------------------- /1-Fixed-Size-Data/02-Functions/readme.txt: -------------------------------------------------------------------------------- 1 | 2 Functions and Programs 2 | 3 | As far as programming is concerned, “arithmetic” is half the game; the other 4 | half is “algebra.” Of course, “algebra” relates to the school notion of 5 | algebra as little/much as the notion of “arithmetic” from the preceding 6 | chapter relates to arithmetic taught in grade-school arithmetic. Specifically, 7 | the algebra notions needed are variable, function definition, function 8 | application, and function composition. 9 | This chapter reacquaints you with these notions in a fun and 10 | accessible manner. 11 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/03-HtDP/01_Designing_Functions.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 01_Designing_Functions) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 3 How to Design Programs 5 | ;; 3.1 Desgining Funtions 6 | ;; Exercises: 33 7 | 8 | ;; See ex033.txt for exercise 33. 9 | 10 | (require 2htdp/image) 11 | 12 | ;; 3.1 Designing Functions 13 | 14 | ;; First we must specify the data 15 | ;; Simple data types: Number, String, Image, Boolean 16 | ;; Information is different than data 17 | ;; You represent Information as Data 18 | ;; You interpret Data as Information 19 | 20 | ;; the Number 42 can mean anything including a Tenperature 21 | 22 | ;; A data definition: 23 | 24 | ;; Temperature is a Number. 25 | ;; interp. represents degrees Celsius 26 | 27 | ; 102 is a Ttemperature because it is a number 28 | ; "cold" is not a Temperature but an interpretation 29 | 30 | 31 | ;; Number -> Number 32 | ;; produce temperature in Celsius given a temparature in Fahrenheit 33 | (define (f2c f) 34 | (* 5/9 (- f 32))) 35 | 36 | 37 | 38 | ;; The Design Process 39 | 40 | ;;; 1. Express how you wish to represent information as data. 41 | 42 | ;; We use numbers to represent centimeters. 43 | 44 | ;;; 2. Write down a signature, a statement of purpost and a function header. 45 | 46 | ;; a. function signature 47 | 48 | ;; String -> Number 49 | 50 | ;; Temperature -> String 51 | 52 | ;; b. A purpose statement 53 | ;; what does the function compute 54 | 55 | 56 | ;; c. A header 57 | 58 | (define (f a-string) 0) 59 | (define (g n) "a") 60 | (define (h num str img) (empty-scene 100 100)) 61 | 62 | ;; an example: 63 | 64 | ;; Number String Image -> Image 65 | ;; add s to img, 66 | ;; y pixels from top, 10 pixels to the left 67 | (define (add-image y s img) 68 | (place-image (text s 10 "red") 10 y img)) 69 | 70 | 71 | ;;; 4. Illustrate the signature and the purpose statements with some 72 | ;; functional exampesl 73 | 74 | ;; Number -> Number 75 | ;; compute the area of a square whose side is len 76 | ;; given: 2, expect: 4 77 | ;; given: 7, expect: 49 78 | ;(define (area-of-square len) 0) ;stub 79 | 80 | ;;; 4. Take inventory. 81 | ;; represent inventory in a template 82 | 83 | ;(define (area-of-square len) 84 | ; (... len ...)) 85 | 86 | ;; now code up the problems 87 | 88 | ;; Number -> Number 89 | ;; computes the area of a square with side len 90 | ;; given: 2, expect: 4 91 | ;; given: 7, expect: 49 92 | (define (area-of-square len) 93 | (sqr len)) 94 | 95 | ;; See add-image.rkt for example from Figure 20. 96 | 97 | ;;; 6. Test the function 98 | (area-of-square 2) ; 4 99 | (area-of-square 7) ; 49 100 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/03-HtDP/02_Finger_Exercises_Functions.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 02_Finger_Exercises_Functions) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e 3 How to Design Functions 5 | ;; 3.2 Finger Exercises: Functions 6 | ;; Exercises 34-38 7 | 8 | ;;See exercises 34, 35, 36, 37, 38 9 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/03-HtDP/05_On_Testing.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 05_On_Testing) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e 3 How to Design Functions 5 | ;; 3.5 On Testing 6 | ;; Exercises 36-40 7 | 8 | ;; Using check-expect helps automate testing 9 | 10 | ;; Figure 21: Testing in BSL 11 | 12 | ; Number -> Number 13 | ; convert Fahrenheit temperatures to Celsius temperatures 14 | 15 | (check-expect (f2c -40) -40) 16 | (check-expect (f2c 32) 0) 17 | (check-expect (f2c 212) 100) 18 | 19 | (check-expect (f2c -40) 40) ;; a wrong example 20 | 21 | (define (f2c f) 22 | (* 5/9 (- f 32))) 23 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/03-HtDP/07_Virtual_Pet_Worlds.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 07_Virtual_Pet_Worlds) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e 3 How to Design Programs 5 | ;; 3.7 Virtual Pet Worlds 6 | 7 | ;; This exercise section introduces the first two elements of a virtual pet game. 8 | ;; It starts with just a display of a cat that keeps walking across the canvas. 9 | ;; Of course, all the walking makes the cat unhappy and its unhappiness shows. 10 | ;; As with all pets, you can try petting, which helps some, or you can try 11 | ;; feeding, which helps a lot more. 12 | 13 | ;; Exercises 45,46,47 14 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/03-HtDP/Word_Program_Template.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname Word_Program_Template) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Figure 22: The wish list for designing world programs 5 | 6 | ;(require 2htdp/image) 7 | (require 2htdp/universe) 8 | 9 | ; WorldState: data representing the current world (cw) 10 | 11 | ; WorldState -> Image 12 | ; when needed, big-bang obtains the image of the current 13 | ; state of the world by evaluating (render cw) 14 | (define (render cw) ...) 15 | 16 | ; WorldState -> WorldState 17 | ; for each tick of the clock, big-bang obtains the next 18 | ; state of the world from (clock-tick-handler cw) 19 | (define (clock-tick-handler cw) ...) 20 | 21 | ; WorldState String -> WorldState 22 | ; for each keystroke, big-bang obtains the next state 23 | ; from (keystroke-handler cw ke); ke represents the key 24 | (define (keystroke-handler cw ke) ...) 25 | 26 | ; WorldState Number Number String -> WorldState 27 | ; for each mouse gesture, big-bang obtains the next state 28 | ; from (mouse-event-handler cw x y me) where x and y are 29 | ; the coordinates of the event and me is its description 30 | (define (mouse-event-handler cw x y me) ...) 31 | 32 | ; WorldState -> Boolean 33 | ; after each event, big-bang evaluates (end? cw) 34 | (define (end? cw) ...) 35 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/03-HtDP/add_image.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname add_image) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; example from Figure 20. 5 | 6 | (require 2htdp/image) 7 | 8 | ;; Number String Image -> Image 9 | ;; adds s to img, y pixels from top, 10 pixels to the left 10 | ;; given: 11 | ;; 5 for y, 12 | ;; "hello" for s, and 13 | ;; (empty-scene 100 100) for img 14 | ;; expected: 15 | ;; (place-image (text "hello" 10 "red") 10 5 ...) 16 | ;; where ... is (empty-scene 100 100) 17 | (define (add-image y s img) 18 | (place-image (text s 10 "red") 10 y img)) 19 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/03-HtDP/allkeys.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname allkeys) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 3 How to Design Programs 5 | ;; 3.6 Designing World Programs 6 | ;; a program to demonstrate keyboard handling 7 | 8 | ;; http://www.ccs.neu.edu/home/matthias/notes/notes/note_mice-and-chars.html 9 | ;; Figure 4: A key event recorder 10 | 11 | (require 2htdp/image) 12 | (require 2htdp/universe) 13 | 14 | ; distances in terms of pixels 15 | (define WIDTH 100) 16 | (define HEIGHT 30) 17 | 18 | ; graphical constant: 19 | (define MT (empty-scene WIDTH HEIGHT)) 20 | 21 | ; An AllKeys is a String. 22 | ; interpretation the keys pressed since big-bang created the canvas 23 | 24 | ; String -> AllKeys 25 | (define (main s) 26 | (big-bang s 27 | [on-key remember] 28 | [to-draw show])) 29 | 30 | 31 | ; AllKeys String -> AllKeys 32 | ; adds ke to ak, the state of the world 33 | 34 | (check-expect (remember "hello" " ") "hello ") 35 | (check-expect (remember "hello " "w") "hello w") 36 | 37 | (define (remember ak ke) 38 | (string-append ak ke)) 39 | 40 | ; AllKeys -> Image 41 | ; renders the string as a text and place it into MT 42 | 43 | (check-expect (show "hel") (overlay (text "hel" 11 "red") MT)) 44 | (check-expect (show "mark") (overlay (text "mark" 11 "red") MT)) 45 | 46 | (define (show ak) 47 | (overlay (text ak 11 "red") MT)) -------------------------------------------------------------------------------- /1-Fixed-Size-Data/03-HtDP/allmouse.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname allmouse) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 3 How to Design Programs 5 | ;; 3.6 Designing World Programs 6 | ;; AllMouseEvts is an element of Image. 7 | 8 | ;; http://www.ccs.neu.edu/home/matthias/notes/notes/note_mice-and-chars.html 9 | ;; Figure 3: A mouse event recorder 10 | 11 | (require 2htdp/image) 12 | (require 2htdp/universe) 13 | 14 | ; graphical constants 15 | (define MT (empty-scene 100 100)) 16 | 17 | 18 | ;; PositiveNumber -> Image 19 | ;; records all mouse events for the specifiec time interval 20 | ;; start the world with: (main 10) for 10 sectonds 21 | (define (main duration) 22 | (big-bang MT 23 | [to-draw show] 24 | [on-tick do-nothing 1 duration] 25 | [on-mouse clack])) 26 | 27 | 28 | ; AllMouseEvts Number Number String -> AllMouseEvts 29 | ; add a dot at (x,y) to ws 30 | 31 | (check-expect 32 | (clack MT 10 20 "something mousy") 33 | (place-image (circle 1 "solid" "red") 10 20 MT)) 34 | 35 | (check-expect 36 | (clack (place-image (circle 1 "solid" "red") 1 2 MT) 3 3 "") 37 | (place-image (circle 1 "solid" "red") 3 3 38 | (place-image (circle 1 "solid" "red") 1 2 MT))) 39 | 40 | (define (clack ws x y action) 41 | (place-image (circle 1 "solid" "red") x y ws)) 42 | 43 | 44 | 45 | ; AllMouseEvts -> AllMouseEvts 46 | ; reveals the current world state (becuase it is an image) 47 | 48 | (check-expect (show MT) MT) 49 | 50 | (define (show ws) 51 | ws) 52 | 53 | 54 | ; AllMouseEvts -> AllMouseEvts 55 | (define (do-nothing ws) ws) -------------------------------------------------------------------------------- /1-Fixed-Size-Data/03-HtDP/ex033.txt: -------------------------------------------------------------------------------- 1 | ;; Ex. 33: 2 | ;; Research the “year 2000” problem and what it meant for software developers. 3 | 4 | It was a lot of work becuase programs weren't desinged for the long term. 5 | Wrong assumptions were made for programs that had a long lifespan. 6 | Date formats were chosen for performance and size on much smaller computers 7 | Also programs weren't designed to be modified so that created more work. 8 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/03-HtDP/ex034.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex034) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 34 5 | ;; Design the function string-first, which extracts the first character from 6 | ;; a non-empty string. Don’t worry about empty strings. 7 | 8 | ;; We use a String to represent random text. The String cannot be empty 9 | 10 | ;; String -> 1String 11 | ;; Given a string extract first charcter 12 | ;; given: "h" expected: "h" 13 | ;; given: "hello world" expected: "h" 14 | ;;(define (string-first str) "") ;stub 15 | 16 | ;(define (sting-first str) ;; template 17 | ; (... str ...)) 18 | 19 | (define (string-first str) 20 | (substring str 0 1)) 21 | 22 | ;; testing 23 | (string-first "h") ; "h" 24 | (string-first "hello world") ; "h" 25 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/03-HtDP/ex035.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex035) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 35: 5 | ;; Design the function string-last, which extracts the last character from a 6 | ;; non-empty string. 7 | 8 | ;; String represents any text 9 | 10 | ;; String -> !String 11 | ;; Given a string extract last charater 12 | ;; given: "h", expect: "h" 13 | ;; given: "hello world", expect: "o" 14 | ;;(define (string-last str) "") ;stub 15 | 16 | ;(define (string-last str) 17 | ; (... str ...)) 18 | 19 | (define (string-last str) 20 | (substring str (- (string-length str) 1))) 21 | 22 | ;; testing 23 | (string-last "h") ; "h" 24 | (string-last "hello world") ; "d" 25 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/03-HtDP/ex036.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex036) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 36: 5 | ;; Design the function image-area, which counts the number of pixels in a 6 | ;; given image. 7 | 8 | ;; Image represents the image we want to anylize 9 | ;; Number represent the area of an image 10 | 11 | (require 2htdp/image) 12 | 13 | ;; Image -> Number 14 | ;; Given an image return area of image in pixils 15 | ;; Given: (square 0 "solid" "blue" expect: 0 16 | ;; Given: (square 10 "solid" "red" expect: 100 17 | ;(define (image-area img) 0) ; stub 18 | 19 | ;(define (image-area img) ; template 20 | ; (... img ...)) 21 | 22 | (define (image-area img) 23 | (* (image-height img) (image-width img))) 24 | 25 | (image-area (square 0 "solid" "blue")) ; 0 26 | (image-area (square 10 "solid" "red")) ; 100 27 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/03-HtDP/ex037.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex037) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 37: 5 | ;; Design the function string-rest, which produces a string like the given 6 | ;; one with the first character removed. 7 | 8 | ;; String represents any text 9 | 10 | ;; String -> String 11 | ;; Given a String return all but first 1String 12 | ;; given: "h", expect: "" 13 | ;; given: "hello", expect "ello" 14 | ;(define (string-rest str) "a") ; stub 15 | 16 | ;(define (string-rest str) 17 | ; (... str ...)) 18 | 19 | (define (string-rest str) 20 | (substring str 1)) 21 | 22 | (string-rest "h") ; "" 23 | (string-rest "hello") ; "ello" 24 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/03-HtDP/ex038.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex038) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 38: 5 | ;; Design the function string-remove-last, which produces a string like the 6 | ;; given one with the last character removed. 7 | 8 | ;; String represents any text. 9 | 10 | ;; String -> String 11 | ;; Given a string remove last char 12 | ;; given "h", expect: "" 13 | ;; given "hello", expect: "hell" 14 | ;(define (string-remove-last str) "") 15 | 16 | ;(define (string-remove-last str) 17 | ; (... str ...)) 18 | 19 | (define (string-remove-last str) 20 | (substring str 0 (- (string-length str) 1))) 21 | 22 | (string-remove-last "h") ; "" 23 | (string-remove-last "hello") ; "hell" 24 | 25 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/03-HtDP/ex039.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex039) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 39: 5 | ;; Develop your favorite image of an automobile so that WHEEL-RADIUS 6 | ;; remains the single point of control. 7 | 8 | (require 2htdp/image) 9 | 10 | ;; Physical Constants; 11 | 12 | (define WHEEL-RADIUS 5) ; single point of controlx 13 | (define WHEEL-OFFSET (* WHEEL-RADIUS 3)) 14 | 15 | ;; Graphical Constants 16 | (define WHEEL 17 | (circle WHEEL-RADIUS "solid" "black")) 18 | 19 | (define SPACE 20 | (rectangle WHEEL-OFFSET WHEEL-RADIUS "solid" "transparent")) 21 | 22 | (define BOTH-WHEELS (beside WHEEL SPACE WHEEL)) 23 | 24 | (define BODY-LENGTH (* WHEEL-RADIUS 10)) 25 | (define BODY-HEIGHT (* WHEEL-RADIUS 2)) 26 | 27 | (define BODY 28 | (above 29 | (rectangle (/ BODY-LENGTH 2) (/ BODY-HEIGHT 2) "solid" "red") 30 | (rectangle BODY-LENGTH BODY-HEIGHT "solid" "red"))) 31 | 32 | ;; a car! 33 | #; 34 | (define CAR (overlay/align/offset 35 | "middle" "bottom" 36 | BOTH-WHEELS 37 | 0 (- 0 WHEEL-RADIUS) 38 | BODY)) 39 | 40 | (define CAR 41 | (overlay/offset BODY 42 | 0 (* WHEEL-RADIUS 1.5) 43 | BOTH-WHEELS)) 44 | 45 | ;; test car 46 | CAR -------------------------------------------------------------------------------- /1-Fixed-Size-Data/03-HtDP/ex040.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex040) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 40: 5 | ;; Formulate the examples as BSL tests. 6 | 7 | ;; WorldState -> WorldState 8 | ;; moves the car by 3 pixels for every clock tick 9 | ;; examples: 10 | ;; given: 20, expect 23 11 | ;; given: 78, expect 81 12 | (define (tock cw) 13 | (+ cw 3)) 14 | 15 | (check-expect (tock 20) 23) 16 | (check-expect (tock 78) 81) 17 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/03-HtDP/ex041.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex041) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 41: 5 | ;; Assemble into a working program 6 | 7 | (require 2htdp/image) 8 | (require 2htdp/universe) 9 | 10 | ;;; Definitions 11 | ;; Physical Constants; 12 | (define WIDTH-OF-WORLD 400) 13 | (define WHEEL-RADIUS 5) ; single point of controlx 14 | (define WHEEL-OFFSET (* WHEEL-RADIUS 3)) 15 | 16 | ;; Graphical Constants 17 | (define WHEEL 18 | (circle WHEEL-RADIUS "solid" "black")) 19 | 20 | (define SPACE 21 | (rectangle WHEEL-OFFSET WHEEL-RADIUS "solid" "transparent")) 22 | 23 | (define BOTH-WHEELS (beside WHEEL SPACE WHEEL)) 24 | 25 | (define BODY-LENGTH (* WHEEL-RADIUS 10)) 26 | (define BODY-HEIGHT (* WHEEL-RADIUS 2)) 27 | 28 | (define BODY 29 | (above 30 | (rectangle (/ BODY-LENGTH 2) (/ BODY-HEIGHT 2) "solid" "red") 31 | (rectangle BODY-LENGTH BODY-HEIGHT "solid" "red"))) 32 | 33 | ;; a car! 34 | (define CAR 35 | (overlay/offset BODY 36 | 0 (* WHEEL-RADIUS 1.5) 37 | BOTH-WHEELS)) 38 | 39 | 40 | (define HEIGHT-OF-WORLD (* (image-height CAR) 1.5)) 41 | (define Y-CAR (- HEIGHT-OF-WORLD (/ (image-height CAR) 2))) 42 | 43 | (define TREE 44 | (underlay/xy (circle 10 "solid" "green") 45 | 9 15 46 | (rectangle 2 20 "solid" "brown"))) 47 | 48 | 49 | (define BACKGROUND 50 | (empty-scene WIDTH-OF-WORLD (* WHEEL-RADIUS 6))) 51 | 52 | 53 | ;;; Data Definition 54 | 55 | ; A WorldState is a Number. 56 | ; the number of pixels between the left border of 57 | ; the scene and the car. 58 | 59 | ;;; Functions 60 | 61 | ;; WorldState -> WorldState 62 | ;; moves the car by 3 pixels for every clock tick 63 | ;; examples: 64 | ;; given: 20, expect 23 65 | ;; given: 78, expect 81 66 | (check-expect (tock 20) 23) 67 | (check-expect (tock 78) 81) 68 | 69 | (define (tock ws) 70 | (+ ws 3)) 71 | 72 | 73 | ;; WorldState -> Image 74 | ; place the car into the BACKGROUND scene, 75 | ;; according to the given world state 76 | (check-expect (render 50) (place-image CAR 50 Y-CAR BACKGROUND)) 77 | (check-expect (render 200) (place-image CAR 200 Y-CAR BACKGROUND)) 78 | 79 | (define (render ws) 80 | (place-image CAR ws Y-CAR BACKGROUND)) 81 | 82 | 83 | ;; WorldState -> Boolean 84 | ;; end car simulation when car reaches end of world 85 | (check-expect (end? 0) #false) 86 | (check-expect (end? 100) #false) 87 | (check-expect (end? (+ WIDTH-OF-WORLD (/ (image-width CAR) 2))) #true) 88 | 89 | (define (end? ws) 90 | (>= ws (- WIDTH-OF-WORLD (/ (image-width CAR) 2)))) 91 | 92 | ;; main : WorldState -> WorldState 93 | ;; launch the program from some initial state 94 | ;; run: (main 0) 95 | (define (main ws) 96 | (big-bang ws 97 | (on-tick tock) 98 | (to-draw render) 99 | (stop-when end?))) 100 | 101 | ;(main 0) 102 | 103 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/03-HtDP/ex042.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex042) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e 3 How to Design Programs 5 | ;; 3.6 Designing World Programs 6 | ;; Exercise 42 7 | 8 | ;; Car sample world 9 | 10 | ;; Ex. 42 11 | ;; Modify the interpretation of the sample data definition so that a state 12 | ;; denotes the x-coordinate of the right-most edge of the car. 13 | 14 | (require 2htdp/image) 15 | (require 2htdp/universe) 16 | 17 | ;;; Definitions 18 | ;; Physical Constants; 19 | (define WIDTH-OF-WORLD 400) 20 | (define WHEEL-RADIUS 5) ; single point of controlx 21 | (define WHEEL-OFFSET (* WHEEL-RADIUS 3)) 22 | 23 | ;; Graphical Constants 24 | (define WHEEL 25 | (circle WHEEL-RADIUS "solid" "black")) 26 | 27 | (define SPACE 28 | (rectangle WHEEL-OFFSET WHEEL-RADIUS "solid" "transparent")) 29 | 30 | (define BOTH-WHEELS (beside WHEEL SPACE WHEEL)) 31 | 32 | (define BODY-LENGTH (* WHEEL-RADIUS 10)) 33 | (define BODY-HEIGHT (* WHEEL-RADIUS 2)) 34 | 35 | (define BODY 36 | (above 37 | (rectangle (/ BODY-LENGTH 2) (/ BODY-HEIGHT 2) "solid" "red") 38 | (rectangle BODY-LENGTH BODY-HEIGHT "solid" "red"))) 39 | 40 | ;; a car! 41 | (define CAR 42 | (overlay/offset BODY 43 | 0 (* WHEEL-RADIUS 1.5) 44 | BOTH-WHEELS)) 45 | 46 | 47 | (define HEIGHT-OF-WORLD (* (image-height CAR) 1.5)) 48 | (define Y-CAR (- HEIGHT-OF-WORLD (/ (image-height CAR) 2))) 49 | 50 | (define TREE 51 | (underlay/xy (circle 10 "solid" "green") 52 | 9 15 53 | (rectangle 2 20 "solid" "brown"))) 54 | 55 | 56 | (define BACKGROUND 57 | (empty-scene WIDTH-OF-WORLD (* WHEEL-RADIUS 6))) 58 | 59 | 60 | ;;; Data Definition 61 | 62 | ; A WorldState is a Number. 63 | ; the number of pixels between the left border of 64 | ; the scene and the right edge of the car. 65 | 66 | ;;; Functions 67 | 68 | ;; WorldState -> WorldState 69 | ;; moves the car by 3 pixels for every clock tick 70 | ;; examples: 71 | ;; given: 20, expect 23 72 | ;; given: 78, expect 81 73 | (check-expect (tock 20) 23) 74 | (check-expect (tock 78) 81) 75 | 76 | (define (tock ws) 77 | (+ ws 3)) 78 | 79 | 80 | ;; WorldState -> Image 81 | ; place the car into the BACKGROUND scene, 82 | ;; according to the given world state 83 | (check-expect (render 50) (place-image CAR (- 50 BODY-LENGTH) Y-CAR BACKGROUND)) 84 | (check-expect (render 100) (place-image CAR (- 100 BODY-LENGTH) Y-CAR BACKGROUND)) 85 | (check-expect (render 200) (place-image CAR (- 200 BODY-LENGTH) Y-CAR BACKGROUND)) 86 | 87 | (define (render ws) 88 | (place-image CAR (- ws BODY-LENGTH) Y-CAR BACKGROUND)) 89 | 90 | 91 | ;; WorldState -> Boolean 92 | ;; end car simulation when car reaches end of world 93 | (check-expect (end? 0) #false) 94 | (check-expect (end? 100) #false) 95 | (check-expect (end? (+ WIDTH-OF-WORLD (/ (image-width CAR) 2))) #true) 96 | 97 | (define (end? ws) 98 | (>= ws (+ WIDTH-OF-WORLD (/ BODY-LENGTH 2)))) 99 | 100 | ;; main : WorldState -> WorldState 101 | ;; launch the program from some initial state 102 | ;; run: (main 0) 103 | (define (main ws) 104 | (big-bang ws 105 | (on-tick tock) 106 | (to-draw render) 107 | (stop-when end?))) 108 | 109 | ;(main 0) 110 | 111 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/03-HtDP/ex045.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex045) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e 3 How to Design Programs 5 | ;; 3.7 Virtual Pet Worlds 6 | 7 | ;;;;;;;;;;;;;;;;;;;;;;;;;;; 8 | ;; Exercise 47 VirtualPet 9 | ;; Design a “virtual cat” world program that continuously moves the cat from left 10 | ;; to right. Let’s call it cat-prog and let’s assume it consumes the starting 11 | ;; position of the cat. Furthermore, make the cat move three pixels per clock 12 | ;; tick. Whenever the cat disappears on the right, it reappears on the left 13 | ;; . You may wish to read up on the modulo function. 14 | 15 | (require 2htdp/image) 16 | (require 2htdp/universe) 17 | 18 | ;; Virual Pet Cat. 19 | 20 | ;; ================= 21 | ;; Constants: 22 | 23 | ; graphical constants 24 | (define W-WIDTH 400) 25 | (define W-HEIGHT 150) 26 | (define DY (/ W-HEIGHT 2)) 27 | (define MT (empty-scene W-WIDTH W-HEIGHT)) 28 | 29 | (define CAT1 (bitmap "images/cat.png")) 30 | 31 | (define SPEED 3) 32 | 33 | ;; ================= 34 | ;; Data definitions: 35 | 36 | ;; WS is Number 37 | ;; interp. WS is the x position from the right. 38 | 39 | 40 | ;; ================= 41 | ;; Functions: 42 | 43 | ;; WS -> WS 44 | ;; start the world with (main 0) 45 | ;; 46 | (define (main ws) 47 | (big-bang ws ; WS 48 | (on-tick tock) ; WS -> WS 49 | (to-draw render) ; WS -> Image 50 | )) 51 | 52 | ;; WS -> WS 53 | ;; produce the next world state 54 | (check-expect (tock 10) 13) 55 | (check-expect (tock W-WIDTH) 0) 56 | 57 | (define (tock ws) 58 | (if (< ws W-WIDTH) 59 | (+ ws SPEED) 60 | 0)) 61 | 62 | 63 | ;; WS -> Image 64 | ;; render the cat on the scene 65 | 66 | ;(check-expect (render 0) (place-image CAT 0 DY MT)) 67 | ;(define (render ws) (place-image CAT ws DY MT)) ;;ex 37 68 | 69 | (check-expect (render 0) (place-image CAT1 0 DY MT)) 70 | (check-expect (render 4) (place-image CAT1 4 DY MT)) 71 | 72 | (define (render ws) 73 | (place-image CAT1 ws DY MT)) 74 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/03-HtDP/ex046.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex046) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e 3 How to Design Programs 5 | ;; 3.7 Virtual Pet Worlds 6 | 7 | ;; Exercise 46 VirtualPet 8 | ;; Improve the cat animation with a slightly different image 9 | ;; Adjust the rendering function from exercise 45 so that it uses one cat image 10 | ;; or the other based on whether the x-coordinate is odd. Read up on odd? in the 11 | ;; HelpDesk, and use a cond expression to select cat images. 12 | 13 | (require 2htdp/image) 14 | (require 2htdp/universe) 15 | 16 | ;; Virual Pet Cat. 17 | 18 | ;; ================= 19 | ;; Constants: 20 | 21 | 22 | ; graphical constants 23 | (define W-WIDTH 400) 24 | (define W-HEIGHT 150) 25 | (define DY (/ W-HEIGHT 2)) 26 | (define MT (empty-scene W-WIDTH W-HEIGHT)) 27 | 28 | (define CAT (bitmap "images/cat.png")) 29 | (define SPEED 3) 30 | 31 | ;; ================= 32 | ;; Data definitions: 33 | 34 | ;; WS is Number 35 | ;; interp. WS is the x position from the right. 36 | 37 | 38 | ;; ================= 39 | ;; Functions: 40 | 41 | ;; WS -> WS 42 | ;; start the world with (main 1) 43 | ;; 44 | (define (main ws) 45 | (big-bang ws ; WS 46 | (on-tick tock) ; WS -> WS 47 | (to-draw render) ; WS -> Image 48 | )) 49 | 50 | ;; WS -> WS 51 | ;; produce the next world state 52 | (check-expect (tock 0) 3) 53 | (check-expect (tock 10) 13) 54 | (check-expect (tock W-WIDTH) 0) 55 | 56 | (define (tock ws) 57 | (if (< ws W-WIDTH) 58 | (+ ws SPEED) 59 | 0)) 60 | 61 | 62 | ;; WS -> Image 63 | ;; render the cat on the scene 64 | ;; Use rotate function instead of two cat images. 65 | ;(check-expect (render 0) (place-image CAT1 0 DY MT)) 66 | ;(define (render ws) (place-image CAT1 ws DY MT)) ;;ex 37 67 | 68 | (check-expect (render 0) (place-image (rotate 5 CAT) 0 DY MT)) 69 | (check-expect (render 4) (place-image (rotate 5 CAT) 4 DY MT)) 70 | (check-expect (render 7) (place-image (rotate -5 CAT) 7 DY MT)) 71 | 72 | (define (render ws) 73 | (if (= (modulo ws 2) 0) 74 | (place-image (rotate 5 CAT) ws DY MT) 75 | (place-image (rotate -5 CAT) ws DY MT))) 76 | 77 | ;; run. 78 | ;; (main 0) 79 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/03-HtDP/ex047.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-intermediate-reader.ss" "lang")((modname 03_07_Virtualpet2) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e 3 How to Design Programs 5 | ;; 2.3.7 Virtual Pet Worlds 6 | 7 | ;; Exercise 47 Pet Guage 8 | 9 | (require 2htdp/image) 10 | (require 2htdp/universe) 11 | 12 | ;; Virtual Pet Simualtion 13 | 14 | ;; ================= 15 | ;; Constants: 16 | 17 | 18 | ; graphical constants 19 | (define W-WIDTH 110) 20 | (define W-HEIGHT 30) 21 | (define G-HEIGHT 10) 22 | (define MT (empty-scene W-WIDTH W-HEIGHT)) 23 | 24 | 25 | ;; ================= 26 | ;; Data definitions: 27 | 28 | ;; CS is a Number 29 | ;; interp. the cat's happiness 30 | (define CS1 0) 31 | (define CS2 50) 32 | (define CS3 100) 33 | 34 | 35 | ;; ================= 36 | ;; Functions: 37 | 38 | ;; CS -> CS 39 | ;; start the world with (main 100) 40 | ;; 41 | (define (main cs) 42 | (big-bang cs ; CS 43 | (on-tick tock) ; CS -> CS 44 | (to-draw render) ; CS -> Image 45 | (on-key cat-attention) ; CS KeyEvent -> CS 46 | )) 47 | 48 | ;; CS -> CS 49 | ;; produce the next CS countdown from 100 50 | ;; range [0, 100] 51 | (check-expect (tock 0) 0) 52 | (check-expect (tock 50) 49.9) 53 | (check-expect (tock 100) 99.9) 54 | 55 | (define (tock cs) 56 | (cond [(<= cs 0) 0] 57 | [(> cs 100) 100] 58 | [else (- cs 0.1)])) 59 | 60 | 61 | ;; CS -> Image 62 | ;; render the cat's happyness guage 63 | (check-expect (render 50) (place-image/align 64 | (rectangle 50 G-HEIGHT "solid" "red") 65 | 5 10 "left" "bottom" 66 | MT)) 67 | (define (render cs) 68 | (place-image/align 69 | (rectangle cs G-HEIGHT "solid" "red") 70 | 5 10 "left" "bottom" 71 | MT)) 72 | 73 | ;; CS KeyEvent -> CS 74 | ;; handle cat's feeding and petting 75 | ;; up arrow pets cat, down arrow feeds cat 76 | 77 | (define (cat-attention cs ke) 78 | (cond [(key=? ke "up") (pet cs)] 79 | [(key=? ke "down") (feed cs)] 80 | [else cs])) 81 | 82 | 83 | ;; CS -> CS 84 | ;; pet the cat 85 | (check-expect (pet 10) 10.5) 86 | 87 | (define (pet cs) (+ cs 0.5)) 88 | 89 | 90 | ;; CS -> CS 91 | ;; feed the cat 92 | (check-expect (feed 10) 12.0) 93 | 94 | (define (feed cs) (+ cs 2.0)) 95 | 96 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/03-HtDP/images/cat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/emaphis/HtDP2e-solutions/88b59b66a2f090dfd07290f16f8686c7e87298bf/1-Fixed-Size-Data/03-HtDP/images/cat.png -------------------------------------------------------------------------------- /1-Fixed-Size-Data/03-HtDP/readme.txt: -------------------------------------------------------------------------------- 1 | Designing Functions - where we start covering the design method 2 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/04-Intervals/01_Programming_with_Conditionals.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 01_Programming_with_Conditionals) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e 1.4 Intervals, Enumerations, etc. 5 | ;; 4.1 Programming with Condinals 6 | 7 | ;(cond 8 | ; [ConditionExpression1 ResultExpression1] 9 | ; [ConditionExpression2 ResultExpression2] 10 | ; ... 11 | ; [ConditionExpressionN ResultExpressionN]) 12 | 13 | ; A function that uses a cond expression. 14 | (define (next traffic-light-state) 15 | (cond 16 | [(string=? "red" traffic-light-state) "green"] 17 | [(string=? "green" traffic-light-state) "yellow"] 18 | [(string=? "yellow" traffic-light-state) "red"])) 19 | 20 | (next"yellow") 21 | 22 | ;; else for all other cases 23 | 24 | ;(cond 25 | ; [ConditionExpression1 ResultExpression1] 26 | ; [ConditionExpression2 ResultExpression2] 27 | ; ... 28 | ; [else DefaultResultExpressionN]) 29 | 30 | 31 | ; A PositiveNumber is a Number greater or equal to 0. 32 | 33 | ; PositiveNumber-> String 34 | ; compute the reward level from the given score s 35 | (define (reward-1 s) 36 | (cond 37 | [(<= 0 s 10) 38 | "bronze"] 39 | [(and (< 10 s) (<= s 20)) 40 | "silver"] 41 | [(< 20 s) 42 | "gold"])) 43 | 44 | (define (reward-2 s) 45 | (cond 46 | [(<= 0 s 10) 47 | "bronze"] 48 | [(and (< 10 s) (<= s 20)) 49 | "silver"] 50 | [else 51 | "gold"])) 52 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/04-Intervals/02_Computing_Conditionaly.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 02_Computing_Conditionaly) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 4 Intervals, Enumerations, etc. 5 | ;; 4.2 Computing Conditionally 6 | ;; Exercises 48,49 7 | 8 | (require 2htdp/image) 9 | 10 | ; A PositiveNumber is a Number greater or equal to 0. 11 | 12 | ;; Unit Tests 13 | (check-expect (reward 5) "bronze") 14 | (check-expect (reward 15) "silver") 15 | (check-expect (reward 35) "gold") 16 | 17 | ; PositiveNumber-> String 18 | ; compute the reward level from the given score s 19 | (define (reward s) 20 | (cond 21 | [(<= 0 s 10) "bronze"] 22 | [(and (< 10 s) (<= s 20)) "silver"] 23 | [else "gold"])) 24 | 25 | ;;; evaluation: 26 | (reward 3) 27 | 28 | (cond 29 | [(<= 0 3 10) "bronze"] 30 | [(and (< 10 3) (<= 3 20)) "silver"] 31 | [else "gold"]) 32 | 33 | (cond 34 | [#true "bronze"] 35 | [(and (< 10 3) (<= 3 20)) "silver"] 36 | [else "gold"]) 37 | 38 | "bronze" 39 | 40 | ;;; a scond example 41 | 42 | (reward 21) 43 | 44 | (cond 45 | [(<= 0 21 10) "bronze"] 46 | [(and (< 10 21) (<= 21 20)) "silver"] 47 | [else "gold"]) 48 | 49 | (cond 50 | [#false "bronze"] 51 | [(and (< 10 21) (<= 21 20)) "silver"] 52 | [else "gold"]) 53 | 54 | (cond 55 | [(and (< 10 21) (<= 21 20)) "silver"] 56 | [else "gold"]) 57 | 58 | 59 | ;; See exercise 48,49 60 | 61 | ;; See rocket1.rkt for reformation exercise 62 | ;; Figure 24. 63 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/04-Intervals/04_Intervals.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 04_Intervals) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 4 Enumerations, Intervals, Itemizations 5 | ;; 4.4 Intervals 6 | ;; Exercises: 52 7 | 8 | 9 | ;; for ufo example see: 04_Ufo.rkt 10 | 11 | 12 | ;;; Examples 13 | 14 | ; A WorldState falls into one of three intervals: 15 | ; – between 0 and CLOSE 16 | ; – between CLOSE and HEIGHT 17 | ; – below HEIGHT 18 | 19 | ;; Template 20 | 21 | (define CLOSE 10) 22 | (define HEIGHT 100) 23 | 24 | ; WorldState -> WorldState 25 | (define (f y) 26 | (cond 27 | [(<= 0 y CLOSE) ...] 28 | [(<= CLOSE y HEIGHT) ...] 29 | [(>= y HEIGHT) ...])) 30 | 31 | ;; More obvious" 32 | ; WorldState -> WorldState 33 | (define (g y) 34 | (cond 35 | [(<= 0 y CLOSE) ...] 36 | [(and (< CLOSE y) (<= y HEIGHT)) ...] 37 | [(> y HEIGHT) ...])) 38 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/04-Intervals/04_Ufo.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 04_Ufo) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 4 Enumerations and Intervals 5 | ;; 4.4 Intervals 6 | ;; ufo example 7 | 8 | (require 2htdp/image) 9 | (require 2htdp/universe) 10 | 11 | 12 | ; WorldState is a Number 13 | ; interperatation: number of pixels between the top and the UFO 14 | 15 | ; A WorldState falls into one of three intervals: 16 | ; – between 0 and CLOSE 17 | ; – between CLOSE and HEIGHT 18 | ; – below HEIGHT 19 | #; 20 | (define (fn-for-ws ws) ;; template 21 | (cond [(<= 0 ws CLOSE) (... ws)] 22 | [(and (< CLOSE ws) (<= ws HEIGHT)) (... ws)] 23 | [(> ws HEIGHT) (...)])) 24 | 25 | 26 | ;; constants: 27 | (define WIDTH 300) 28 | (define HEIGHT 100) 29 | (define CLOSE (/ HEIGHT 3)) 30 | 31 | ;; visual constants: 32 | (define MTSCN (empty-scene WIDTH HEIGHT)) 33 | 34 | (define UFO 35 | (overlay (circle 10 "solid" "green") 36 | (rectangle 40 2 "solid" "green"))) 37 | 38 | 39 | 40 | ;; functions 41 | 42 | ; run program 43 | ; WorldState -> WorldState 44 | ;; run: (main 0) 45 | (define (main y0) 46 | (big-bang y0 47 | [on-tick nxt .2] 48 | [to-draw render])) 49 | 50 | 51 | ;; WorldState -> WorldState 52 | ;; compute next location of UFO 53 | (check-expect (nxt 11) 14) 54 | 55 | (define (nxt y) 56 | (+ y 3)) 57 | 58 | 59 | ; WorldState -> Image 60 | ; place UFO at given height into the center of MTSCN 61 | (check-expect (render 11) 62 | (place-image UFO (/ WIDTH 2) 11 MTSCN)) 63 | 64 | (define (render y) 65 | (place-image UFO (/ WIDTH 2) y MTSCN)) 66 | 67 | 68 | ;; Sample Problem -- Status Line 69 | 70 | ; WorldState -> Image 71 | ; add a status line to the scene create by render 72 | 73 | ;; Figure 27. 74 | (check-expect (render/status 10) 75 | (place-image (text "descending" 11 "green") 76 | 20 20 77 | (render 10))) 78 | (check-expect (render/status 42) 79 | (place-image (text "closing in" 11 "orange") 80 | 20 20 81 | (render 42))) 82 | (check-expect (render/status 101) 83 | (place-image (text "landed" 11 "red") 84 | 20 20 85 | (render 101))) 86 | 87 | (define (render/status y) 88 | (place-image 89 | (cond 90 | [(<= 0 y CLOSE) 91 | (text "descending" 11 "green")] 92 | [(and (< CLOSE y) (<= y HEIGHT)) 93 | (text "closing in" 11 "orange")] 94 | [(> y HEIGHT) 95 | (text "landed" 11 "red")]) 96 | 20 20 97 | (render y))) 98 | 99 | 100 | ; WorldState -> WorldState 101 | (define (main1 y0) 102 | (big-bang y0 103 | [on-tick nxt .2] 104 | [to-draw render/status])) 105 | 106 | ; (main1 0) 107 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/04-Intervals/06_Designing_with_Itemizations.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 06_Designing_with_Itemizations) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 4 Enumerations, Intervals, Itemizations 5 | ;; 4.6 Designing with Itemizations 6 | ;; Exercise: 58 7 | 8 | ;; Sales tax example 9 | 10 | ;; Sample Problem The state of Tax Land has created a three-stage sales 11 | ;; tax to cope with its budget deficit. 12 | ;; - Inexpensive items, those costing less than $1,000, are not taxed. 13 | ;; - Luxury items, with a price of morethan $10,000, are taxed at the 14 | ;; rate of eight percent (8.00%). 15 | ;; - Everything in between comes with a five percent (5%) mark up. 16 | 17 | ;; Design a function for a cash register that given the price of an item, 18 | ;; computes the sales tax. 19 | 20 | ;; 1. Data definition: 21 | 22 | ; A Price falls into one of three intervals: 23 | ; — 0 through 1000; 24 | ; — 1000 through 10000; 25 | ; — 10000 and above. 26 | ; interpretation the price of an item 27 | 28 | ;; sample data 29 | (define PRC1 0) 30 | (define PRC2 500) 31 | (define PRC3 1000) 32 | 33 | ;; Template for Price 34 | #; 35 | (define (fn-for-st st) 36 | (cond 37 | [(and (<= 0 st) (< st 1000)) (... st)] 38 | [(and (<= 1000 st) (< st 10000)) (... st)] 39 | [(>= st 10000) (... st)])) 40 | 41 | 42 | ;; 2. Signature, purpose statement and function hearder 43 | 44 | ; Price -> Number 45 | ; computes the amount of tax charged for price p 46 | ;(define (sales-tax p) 0.00) ;stub 47 | 48 | ;; 3. Function examples 49 | 50 | ;; 0.00 => 0.00 51 | ;; 537.00 => 0.00 52 | ;; 1000.00 => (* 0.05 1000.00) => 50.00 53 | ;; 10000.00 => (* 0.08 10000.00) => 800.00 54 | 55 | (check-expect (sales-tax 0.00) 0.00) 56 | (check-expect (sales-tax 537.00) 0.00) 57 | (check-expect (sales-tax 1000.00) (* 0.05 1000.00)) 58 | (check-expect (sales-tax 10000.00) (* 0.08 10000.00)) 59 | (check-expect (sales-tax 12017.00) 961.36) 60 | 61 | ;; 4. The template based on classes of data 62 | 63 | #; 64 | (define (sales-tax-template p) ;template for sales-tax 65 | (cond 66 | [(and (<= 0 p) (< p 1000)) ...] 67 | [(and (<= 1000 p) (< p 10000)) ...] 68 | [(>= p 10000) ...])) 69 | 70 | 71 | ;; Function definition 72 | 73 | (define (sales-tax p) 74 | (cond 75 | [(and (<= 0 p) (< p 1000)) 0] 76 | [(and (>= p 1000) (< p 10000)) (* 0.05 p)] 77 | [(>= p 10000) (* 0.08 p)])) 78 | 79 | ;; See exercise 58. 80 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/04-Intervals/ex048.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex048) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex 48. 5 | ;; Enter the definition of reward followed by (reward 18) into the definitions 6 | ;; area of DrRacket and use the stepper to find out how DrRacket evaluates 7 | ;; applications of the function. 8 | 9 | ; PositiveNumber-> String 10 | ; compute the reward level from the given score s 11 | ;; Unit Tests 12 | (check-expect (reward 5) "bronze") 13 | (check-expect (reward 15) "silver") 14 | (check-expect (reward 35) "gold") 15 | 16 | (define (reward s) 17 | (cond 18 | [(<= 0 s 10) "bronze"] 19 | [(and (< 10 s) (<= s 20)) "silver"] 20 | [else "gold"])) 21 | 22 | (reward 18) 23 | 24 | (cond 25 | [(<= 0 18 10) "bronze"] 26 | [(and (< 10 18) (<= 18 20)) "silver"] 27 | [else "gold"]) 28 | 29 | (cond 30 | [#false "bronze"] 31 | [(and (< 10 18) (<= 18 20)) "silver"] 32 | [else "gold"]) 33 | 34 | (cond 35 | [(and (< 10 18) (<= 18 20)) "silver"] 36 | [else "gold"]) 37 | 38 | (cond 39 | [(and #true (<= 18 20)) "silver"] 40 | [else "gold"]) 41 | 42 | (cond 43 | [(and #true #true) "silver"] 44 | [else "gold"]) 45 | 46 | (cond 47 | [#true "silver"] 48 | [else "gold"]) 49 | 50 | "silver" -------------------------------------------------------------------------------- /1-Fixed-Size-Data/04-Intervals/ex049.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex049) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 49: 5 | ;; (- 200 (cond [(> y 200) 0] [else y])) 6 | ;; Use the stepper to evaluate the expression for y as 100 and 210. 7 | 8 | (- 200 (cond [(> 100 200) 0] [else 100])) 9 | (- 200 (cond [#false 0] [else 100])) 10 | (- 200 (cond [else 100])) 11 | (- 200 100) 12 | 100 13 | 14 | (- 200 (cond [(> 210 200) 0] [else 210])) 15 | (- 200 (cond [#true 0] [else 210])) 16 | (- 200 0) 17 | 200 18 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/04-Intervals/ex050.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex50) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex 50. 5 | ;; If you copy and paste the above function definition into 6 | ;; the definitions area of DrRacket and click RUN, DrRacket 7 | ;; highlights two of the three cond lines. 8 | ;; This coloring tells you that your test cases do not cover the 9 | ;; full conditional. Add enough tests to make DrRacket happy. 10 | 11 | ;; a function consumming TrafficLight 12 | 13 | ; TrafficLight -> TrafficLight 14 | ; yields the next state given current state s 15 | (check-expect (traffic-light-next "red") "green") 16 | (check-expect (traffic-light-next "green") "yellow") ; Ex. 50 17 | (check-expect (traffic-light-next "yellow") "red") ; Ex. 50 18 | 19 | (define (traffic-light-next s) 20 | (cond 21 | [(string=? "red" s) "green"] 22 | [(string=? "green" s) "yellow"] 23 | [(string=? "yellow" s) "red"])) 24 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/04-Intervals/ex051.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex51) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 51: 5 | ;; Design a big-bang program that simulates a traffic light for 6 | ;; a given duration. The program renders the state of a traffic light 7 | ;; as a solid circle of the appropriate color, and it changes state 8 | ;; on every clock tick. 9 | ;; What is the most appropriate initial state? Ask your engineering friends 10 | 11 | (require 2htdp/image) 12 | (require 2htdp/universe) 13 | 14 | ; graphical constants 15 | (define W-WIDTH 100) 16 | (define W-HEIGHT 100) 17 | (define DX (/ W-WIDTH 2)) 18 | (define DY (/ W-HEIGHT 2)) 19 | (define MT (empty-scene W-WIDTH W-HEIGHT)) 20 | 21 | ;; TraficLight -> Image 22 | ;; given state s, return color light 23 | (check-expect (display-light "red") (circle 35 "solid" "red")) 24 | 25 | ;;(define (display-light s) (square 0 "solid" "white")) ;; Stub 26 | (define (display-light s) (circle 35 "solid" s)) 27 | 28 | 29 | ;; TraficLight -> TraficLight 30 | ;; start the world with (main "yellow") -- to start red. 31 | ;; 32 | (define (main ws) 33 | (big-bang ws ; TraficLight 34 | (on-tick tock 3) ; TraficLight -> TraficLight (every 3 seconds) 35 | (to-draw render) ; TraficLight -> Image 36 | )) 37 | 38 | 39 | ; TrafficLight -> TrafficLight 40 | ; yields the next s 41 | (check-expect (traffic-light-next "red") "green") 42 | (check-expect (traffic-light-next "green") "yellow") ; Ex. 50 43 | (check-expect (traffic-light-next "yellow") "red") ; Ex. 50 44 | 45 | (define (traffic-light-next s) 46 | (cond 47 | [(string=? "red" s) "green"] 48 | [(string=? "green" s) "yellow"] 49 | [(string=? "yellow" s) "red"])) 50 | 51 | ;; TraficLight -> TraficLight 52 | ;; produce the next traffic light state 53 | (check-expect (tock "red") "green") 54 | (check-expect (tock "green") "yellow") 55 | (check-expect (tock "yellow") "red") 56 | 57 | (define (tock tl) 58 | (traffic-light-next tl)) 59 | 60 | ;; TraficLight -> Image 61 | ;; render the traffic light 62 | (check-expect (render "red") (place-image (display-light "red") DX DY MT)) 63 | 64 | (define (render ws) 65 | (place-image (display-light ws) DX DY MT)) 66 | 67 | ; test 68 | ;(main "yellow") -------------------------------------------------------------------------------- /1-Fixed-Size-Data/04-Intervals/ex052.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex052) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 52. 5 | ;; Which integers are contained in the four intervals above? 6 | 7 | ; [3,5] => 3,4,5 8 | ; (3,5] => 4,5 9 | ; [3,5) => 3,4 10 | ; (3,5) => 4 11 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/04-Intervals/ex053.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex053) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 53: 5 | ;; In some way it is best to draw some world scenarios and to represent 6 | ;; them with data and, conversely, to pick some data examples and to 7 | ;; draw pictures that match them. Do so for the LR definition, including 8 | ;; at least HEIGHT and 0 as examples. 9 | 10 | (require 2htdp/image) 11 | 12 | ;; Datad 13 | 14 | ; physical constants 15 | (define HEIGHT 100) 16 | (define WIDTH 100) 17 | 18 | ; graphical constants 19 | (define SCENE (empty-scene WIDTH HEIGHT)) 20 | (define ROCKET (bitmap "images/rocket.png")) ; use your favorite image 21 | 22 | (define ROCKET-HEIGHT (image-height ROCKET)) 23 | (define ROCKET-CENTER (- HEIGHT (/ ROCKET-HEIGHT 2))) 24 | (define CENTER-Y (/ WIDTH 2)) 25 | 26 | ; A LR (short for: launching rocket) is one of: 27 | ; – "resting" 28 | ; – NonnegativeNumber 29 | ; interpretation "resting" represents a grounded rocket 30 | ; a number denotes the height of a rocket in flight. 31 | 32 | 33 | ROCKET-HEIGHT 34 | ;; some mock-up images 35 | (place-image ROCKET CENTER-Y (- HEIGHT (/ ROCKET-HEIGHT 2)) SCENE) ; "resting" 36 | (place-image ROCKET CENTER-Y (/ HEIGHT 2) SCENE) ; almost half way 37 | (place-image ROCKET CENTER-Y 0 SCENE) ; almost all the way 38 | (place-image ROCKET CENTER-Y HEIGHT SCENE) ; all the way up 39 | 40 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/04-Intervals/ex054.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex054) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 54: 5 | ;; Why is (string=? "resting" x) incorrect as the first condition in 6 | ;; show? Conversely, formulate a completely accurate condition, that is, 7 | ;; a Boolean expression that evaluates to #true precisely when x belongs 8 | ;; to the first sub-class of LRCD. 9 | 10 | ;; (string=? "resting" x) is inaccurate because any string can be 11 | ;; concidered a resting state also an LRCD can be any number. 12 | . 13 | ;; a better test 14 | (and (string? x) (string=? "resting" x)) 15 | 16 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/04-Intervals/ex055.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex055) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; ex55. 5 | ;;Take another look at show. It contains three instances of an expression 6 | ;; with the approximate shape: 7 | ;;(place-image ROCKET 10 (- ... CENTER) BACKG) 8 | 9 | ;; This expression appears three times in the function: twice to draw a resting rocket and once to draw a flying rocket. Define an auxiliary function that performs this work and thus shorten show. Why is this a good idea? 10 | 11 | (require 2htdp/image) 12 | (require 2htdp/universe) 13 | 14 | ;; Constants 15 | 16 | (define HEIGHT 300) ; distances in pixels 17 | (define WIDTH 100) 18 | 19 | (define BACKG (empty-scene WIDTH HEIGHT)) 20 | (define ROCKET (rectangle 5 30 "solid" "red")) 21 | (define CENTER (/ (image-height ROCKET) 2)) 22 | 23 | ;; Function definition 24 | 25 | ; LRCD -> Image 26 | ; renders the state as a resting or flying rocket 27 | 28 | (check-expect 29 | (show HEIGHT) 30 | (place-image ROCKET 10 (- HEIGHT CENTER) BACKG)) 31 | 32 | (check-expect 33 | (show -2) 34 | (place-image (text "-2" 20 "red") 35 | 10 (* 3/4 WIDTH) 36 | (place-image ROCKET 10 (- HEIGHT CENTER) BACKG))) 37 | 38 | (check-expect 39 | (show 53) 40 | (place-image ROCKET 10 (- 53 CENTER) BACKG)) 41 | 42 | ;(define (show x) ; stub 43 | ; SCENE) 44 | 45 | ;; Ex. 55: 46 | ;; an axiliary function: 47 | 48 | ;; Integer -> Image 49 | ;; render the rocket image at a given height 50 | (check-expect (render-rocket 10) 51 | (place-image ROCKET 10 (- 10 CENTER) BACKG)) 52 | 53 | (define (render-rocket x) 54 | (place-image ROCKET 10 (- x CENTER) BACKG)) 55 | 56 | ;; new show using render-rocket. 57 | (define (show x) 58 | (cond 59 | [(string? x) 60 | (render-rocket HEIGHT)] 61 | [(<= -3 x -1) 62 | (place-image (text (number->string x) 20 "red") 63 | 10 (* 3/4 WIDTH) 64 | (render-rocket HEIGHT))] 65 | [(>= x 0) 66 | (render-rocket x)])) 67 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/04-Intervals/ex057.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex057) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex 57: 5 | ;; Recall that the word “height” forced us to choose one of two possible 6 | ;; interpretation. Now that you have solved the exercises in this section, 7 | ;; solve them again using the first interpretation of the word. 8 | ;; Compare and contrast the solutions. 9 | 10 | (require 2htdp/image) 11 | (require 2htdp/universe) 12 | 13 | 14 | ;; Constants Definitions 15 | 16 | (define WIDTH 100) ; pixels 17 | (define HEIGHT 200) ; pixels 18 | (define SCENE (empty-scene WIDTH HEIGHT)) 19 | 20 | (define ROCKET (bitmap "./images/rocket.png")) 21 | (define ROCKET-CENTER (/ (image-height ROCKET) 2)) 22 | (define ROCKET-X (/ WIDTH 2)) 23 | (define ROCKET-GROUNDED-RL ROCKET-CENTER) 24 | (define ROCKET-VELOCITY 3) ; pixels per clock tick 25 | 26 | 27 | ;; RocketLaunch -> Image 28 | ;; Renders the state as a resting or flying rocket image. 29 | (check-expect (show "resting") (draw-rocket ROCKET-GROUNDED-RL)) 30 | (check-expect (show -2) (place-image 31 | (text "-2" 20 "red") 32 | 10 (* 3/4 WIDTH) 33 | (draw-rocket ROCKET-GROUNDED-RL))) 34 | (check-expect (show 53) (draw-rocket (+ 53 ROCKET-CENTER))) 35 | (check-expect (show HEIGHT) (draw-rocket (+ HEIGHT ROCKET-CENTER))) 36 | (define (show rl) 37 | (cond 38 | [(string? rl) 39 | (draw-rocket ROCKET-GROUNDED-RL)] 40 | [(<= -3 rl -1) 41 | (place-image (text (number->string rl) 20 "red") 42 | 10 (* 3/4 WIDTH) 43 | (draw-rocket ROCKET-GROUNDED-RL))] 44 | [(>= rl 0) 45 | (draw-rocket (+ rl ROCKET-CENTER))])) 46 | 47 | 48 | 49 | ;; RocketLaunch -> Image 50 | ;; An auxiliary function that draws a rocket image. 51 | (check-expect (draw-rocket 50) (place-image ROCKET ROCKET-X (- HEIGHT 50) SCENE)) 52 | (define (draw-rocket rl) 53 | (place-image ROCKET ROCKET-X (- HEIGHT rl) SCENE)) 54 | 55 | ;; RocketLaunch KeyEvent -> RocketLaunch 56 | ;; Starts the contdown when the space bar is pressed, 57 | ;; if the rocket is still resting. 58 | (check-expect (launch "resting" " ") -3) 59 | (check-expect (launch "resting" "a") "resting") 60 | (check-expect (launch -3 " ") -3) 61 | (check-expect (launch -1 " ") -1) 62 | (check-expect (launch 33 " ") (- HEIGHT 33)) 63 | (check-expect (launch 33 "a") (- HEIGHT 33)) 64 | (define (launch rl key-event) 65 | (cond 66 | [(string? rl) (if (string=? " " key-event) -3 rl)] 67 | [(<= -3 rl -1) rl] 68 | [(>= rl 0) (- HEIGHT rl)])) 69 | 70 | 71 | ;; RocketLaunch -> RocketLaunch 72 | ;; Raises the rocket by ROCKET-VELOCITY 73 | ;; if it is moving already. 74 | (check-expect (fly "resting") "resting") 75 | (check-expect (fly -3) -2) 76 | (check-expect (fly -2) -1) 77 | (check-expect (fly -1) 0) 78 | (check-expect (fly 10) (+ 10 ROCKET-VELOCITY)) 79 | (check-expect (fly 22) (+ 22 ROCKET-VELOCITY)) 80 | (define (fly rl) 81 | (cond 82 | [(string? rl) rl] 83 | [(<= -3 rl -1) (if (= rl -1) 0 (+ rl 1))] 84 | [(>= rl 0) (+ rl ROCKET-VELOCITY)])) 85 | 86 | 87 | ;; RocketLaunch -> RocketLaunch 88 | (define (main rl) 89 | (big-bang rl 90 | [to-draw show] 91 | [on-key launch] 92 | [on-tick fly 0.2])) 93 | 94 | 95 | ;; test 96 | ;(main "resting") 97 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/04-Intervals/ex058.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex058) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | 5 | ;; Ex. 58: 6 | ;; Introduce constant definitions that separate the intervals for low 7 | ;; prices and luxury prices from the others so that the legislator in 8 | ;; Tax Land can easily raise the taxes even more. 9 | 10 | ;; 1. Data definition: 11 | 12 | ; A Price falls into one of three intervals: 13 | ; — 0 through 1000; 14 | ; — 1000 through 10000; 15 | ; — 10000 and above. 16 | ; interpretation the price of an item 17 | 18 | (define BASE-LEVEL 0.00) 19 | (define MID-LEVEL 1000.00) 20 | (define HIGH-LEVEL 10000.00) 21 | 22 | (define BASE-TAX 0.00) 23 | (define MID-TAX 0.05) 24 | (define HIGH-TAX 0.08) 25 | 26 | ; Price -> Number 27 | ; compute the amount of tax charged for price p 28 | (check-expect (sales-tax BASE-LEVEL) (* BASE-TAX BASE-LEVEL)) 29 | (check-expect (sales-tax 537.00) (* BASE-TAX 537.00)) 30 | (check-expect (sales-tax MID-LEVEL) (* MID-TAX MID-LEVEL)) 31 | (check-expect (sales-tax 1282.00) (* MID-TAX 1282.00)) 32 | (check-expect (sales-tax HIGH-LEVEL) (* HIGH-TAX HIGH-LEVEL)) 33 | (check-expect (sales-tax 12017.00) (* HIGH-TAX 12017.00)) 34 | 35 | #; 36 | (define (sales-tax-template p) ;template for sales-tax 37 | (cond 38 | [(and (<= BASE-LEVEL p) (< MID-LEVEL 1000)) ...] 39 | [(and (<= MID-LEVEL p) (< p HIGH-LEVEL)) ...] 40 | [(>= p HIGH-LEVEL) ...])) 41 | 42 | 43 | (define (sales-tax p) 44 | (cond 45 | [(and (<= BASE-LEVEL p) (< p MID-LEVEL)) (* BASE-TAX p)] 46 | [(and (>= p MID-LEVEL) (< p HIGH-LEVEL)) (* MID-TAX p)] 47 | [(>= p HIGH-LEVEL) (* HIGH-TAX p)])) 48 | 49 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/04-Intervals/ex062.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex062) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 4 Enumerations and Intervals 5 | ;; 4.7 Finite State Machines 6 | ;; Ex 62. 7 | 8 | 9 | ;; During a door simulation the “open” state is barely visible. Modify 10 | ;; door-simulation so that the clock ticks once every three seconds. 11 | ;; Re-run the simulation. 12 | 13 | ;; door state example (state machine) 14 | 15 | (require 2htdp/image) 16 | (require 2htdp/universe) 17 | 18 | ;;; data 19 | 20 | ; A DoorState is one of: 21 | ; – LOCKED 22 | ; – CLOSED 23 | ; – OPEN 24 | 25 | (define LOCKED "locked") 26 | (define CLOSED "closed") 27 | (define OPEN "open") 28 | 29 | ;; KeyEvent ; key presses 30 | (define UNLOCK "u") 31 | (define LOCK "l") 32 | (define PUSH " ") ; space bar 33 | 34 | 35 | #; 36 | (define (fun-for-ds state-of-door) ; template 37 | (cond 38 | [(string=? LOCKED state-of-door) ...] 39 | [(string=? CLOSED state-of-door) ...] 40 | [(string=? OPEN state-of-door) ...])) 41 | 42 | 43 | 44 | ;; functions 45 | 46 | ; DoorState -> DoorState 47 | ; closes an open door over the period of one tick 48 | (check-expect (door-closer LOCKED) LOCKED) 49 | (check-expect (door-closer CLOSED) CLOSED) 50 | (check-expect (door-closer OPEN) CLOSED) 51 | ;(define (door-closer state-of-door) state-of-door) ;stub 52 | 53 | (define (door-closer state-of-door) 54 | (cond 55 | [(string=? LOCKED state-of-door) LOCKED] 56 | [(string=? CLOSED state-of-door) CLOSED] 57 | [(string=? OPEN state-of-door) CLOSED])) 58 | 59 | 60 | ; DoorState KeyEvent -> DoorState 61 | ; three key events simulate actions on the door 62 | 63 | (check-expect (door-actions LOCKED "u") CLOSED) 64 | (check-expect (door-actions CLOSED "l") LOCKED) 65 | (check-expect (door-actions CLOSED " ") OPEN) 66 | (check-expect (door-actions OPEN "a") OPEN) 67 | (check-expect (door-actions CLOSED "a") CLOSED) 68 | 69 | ;(define (door-actions ds ke) ds) ;stub 70 | 71 | (define (door-actions s k) 72 | (cond 73 | [(and (string=? LOCKED s) (string=? "u" k)) 74 | CLOSED] 75 | [(and (string=? CLOSED s) (string=? "l" k)) 76 | LOCKED] 77 | [(and (string=? CLOSED s) (string=? " " k)) 78 | OPEN] 79 | [else s])) 80 | 81 | 82 | ; DoorState -> Image 83 | ; translates the state s into a large text image 84 | (check-expect (door-render CLOSED) 85 | (text CLOSED 40 "red")) 86 | 87 | (define (door-render s) 88 | (text s 40 "red")) 89 | 90 | 91 | ;; DoorState -> DoorState 92 | ;; simulate a door with an automatic door closer 93 | ;; run with: (door-simulation CLOSED) 94 | (define (door-simulation initial-state) 95 | (big-bang initial-state 96 | [on-tick door-closer 3] ;; Ex. 64 97 | [on-key door-actions] 98 | [to-draw door-render])) 99 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/04-Intervals/images/rocket.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/emaphis/HtDP2e-solutions/88b59b66a2f090dfd07290f16f8686c7e87298bf/1-Fixed-Size-Data/04-Intervals/images/rocket.png -------------------------------------------------------------------------------- /1-Fixed-Size-Data/04-Intervals/readme.txt: -------------------------------------------------------------------------------- 1 | Now we discuse three different data descriptions: enumerations, intervals and itemizations. 2 | 3 | - An enumeration lists every single piece of data that belongs to it, 4 | 5 | - Interval specifies a range of data. 6 | 7 | - Itemizations mixes the first two. -------------------------------------------------------------------------------- /1-Fixed-Size-Data/04-Intervals/rocket1.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname rocket1) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; reformulation exercise: 5 | ;; figure 24. 6 | 7 | (define WIDTH 100) 8 | (define HEIGHT 60) 9 | (define MTSCN (empty-scene WIDTH HEIGHT)) 10 | (define ROCKET (circle 10 "solid" "blue")) 11 | (define ROCKET-CENTER-TO-TOP 12 | (- HEIGHT (/ (image-height ROCKET) 2))) 13 | 14 | (define (create-rocket-scene.v5 h) 15 | (cond 16 | [(<= h ROCKET-CENTER-TO-TOP) 17 | (place-image ROCKET 50 h MTSCN)] 18 | [(> h ROCKET-CENTER-TO-TOP) 19 | (place-image ROCKET 50 ROCKET-CENTER-TO-TOP MTSCN)])) 20 | 21 | ;; nesting the cond expression in a another expression for simplicfication 22 | ;; place-image is only executed once. 23 | (define (create-rocket-scene.v6 h) 24 | (place-image ROCKET 50 25 | (cond 26 | [(<= h ROCKET-CENTER-TO-TOP) h] 27 | [(> h ROCKET-CENTER-TO-TOP) ROCKET-CENTER-TO-TOP]) 28 | MTSCN)) 29 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/04-Intervals/rocket2.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname rocket2) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 4 Enumerations, Intervals, Itemizations 5 | ;; 4.5 Itemizations 6 | ;; Rocket experiment 7 | 8 | #| 9 | Sample Problem 10 | Design a program that launches a rocket when the user presses 11 | the space bar. At that point, the simulation starts a countdown for 12 | three ticks, before it displays the scenery of a rising rocket. 13 | The rocket should move upward at a rate of three pixels per clock tick. 14 | |# 15 | 16 | ;; version using (define CENTER (- HEIGHT (/ (image-height ROCKET) 2))) 17 | ;; This center seems to be more of a formula for a landed rocket 18 | 19 | (require 2htdp/image) 20 | (require 2htdp/universe) 21 | 22 | ;; Physical constants 23 | 24 | (define HEIGHT 200) ; distances in pixels 25 | (define WIDTH 100) 26 | (define YDELTA 3) 27 | 28 | (define BACKG (empty-scene WIDTH HEIGHT)) 29 | (define ROCKET (rectangle 5 30 "solid" "red")) 30 | 31 | ;(define CENTER (- HEIGHT (/ (image-height ROCKET) 2))) 32 | (define CENTER (/ (image-height ROCKET) 2)) 33 | 34 | 35 | ; A LRCD (for launching rocket count down) is one of: 36 | ; – "resting" 37 | ; – a Number between -3 and -1 38 | ; – a NonnegativeNumber 39 | ; interpretation a grounded rocket, in count-down mode, 40 | ; a number denotes the number of pixels between the 41 | ; top of the canvas and the rocket (its height) 42 | 43 | ;; Functions 44 | 45 | ; LRCD -> Image 46 | ; renders the state as a resting or flying rocket 47 | 48 | (check-expect 49 | (show "resting") 50 | (place-image ROCKET 10 CENTER BACKG)) 51 | 52 | (check-expect 53 | (show -2) 54 | (place-image (text "-2" 20 "red") 55 | 10 (* 3/4 WIDTH) 56 | (place-image ROCKET 10 (- HEIGHT CENTER) BACKG))) 57 | 58 | (check-expect ; just off the screen 59 | (show HEIGHT) 60 | (place-image ROCKET 10 (- HEIGHT CENTER) BACKG)) 61 | 62 | (check-expect 63 | (show 53) 64 | (place-image ROCKET 10 (- 53 CENTER) BACKG)) 65 | 66 | 67 | 68 | ;(define (show x) ; stub 69 | ; (place-image (square 10 "solid" "blue") 10 53 BACKG)); 70 | 71 | (define (show x) 72 | (cond 73 | [(string? x) 74 | (place-image ROCKET 10 (- CENTER 0) BACKG)] 75 | [(<= -3 x -1) 76 | (place-image (text (number->string x) 20 "red") 77 | 10 (* 3/4 WIDTH) 78 | (place-image ROCKET 79 | 10 (- CENTER 0) 80 | BACKG))] 81 | [(>= x 0) 82 | (place-image ROCKET 10 (- CENTER x) BACKG)])) 83 | 84 | 85 | ; LRCD KeyEvent -> LRCD 86 | ; starts the count-down when space bar is pressed, 87 | ; if the rocket is still resting 88 | (define (launch x ke) 89 | x) 90 | 91 | ; LRCD -> LRCD 92 | ; raises the rocket by YDELTA, 93 | ; if it is moving already 94 | (define (fly x) 95 | x) 96 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/04-Intervals/rocket3.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname rocket3) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 4 Enumerations, Intervals, Itemizations 5 | ;; 4.5 Itemizations 6 | ;; Rocket experiment 7 | 8 | ;; version using (define CENTER (/ (image-height ROCKET) 2)) 9 | ;; The formula for the check-expects and "show" are very different 10 | ;; from the book 11 | 12 | (require 2htdp/image) 13 | (require 2htdp/universe) 14 | 15 | ;; Physical constants 16 | 17 | (define HEIGHT 300) ; distances in pixels 18 | (define WIDTH 100) 19 | (define YDELTA 3) 20 | 21 | (define BACKG (empty-scene WIDTH HEIGHT)) 22 | (define ROCKET (rectangle 5 30 "solid" "red")) 23 | 24 | ;(define CENTER (- HEIGHT (/ (image-height ROCKET) 2))) 25 | (define CENTER (/ (image-height ROCKET) 2)) 26 | 27 | 28 | ; A LRCD (for launching rocket count down) is one of: 29 | ; – "resting" 30 | ; – a Number between -3 and -1 31 | ; – a NonnegativeNumber 32 | ; interpretation a grounded rocket, in count-down mode, 33 | ; a number denotes the number of pixels between the 34 | ; top of the canvas and the rocket (its height) 35 | 36 | ;; Functions 37 | 38 | ; LRCD -> Image 39 | ; renders the state as a resting or flying rocket 40 | 41 | (check-expect 42 | (show "resting") 43 | (place-image ROCKET 10 (- HEIGHT CENTER) BACKG)) 44 | 45 | (check-expect 46 | (show -2) 47 | (place-image (text "-2" 20 "red") 48 | 10 (* 3/4 WIDTH) 49 | (place-image ROCKET 10 (- HEIGHT CENTER) BACKG))) 50 | 51 | (check-expect ; just off the screen 52 | (show HEIGHT) 53 | (place-image ROCKET 10 (- HEIGHT CENTER HEIGHT 10) BACKG)) 54 | 55 | (check-expect 56 | (show 53) 57 | (place-image ROCKET 10 (- HEIGHT CENTER 53) BACKG)) 58 | 59 | 60 | 61 | ;(define (show x) ; stub 62 | ; (place-image (square 10 "solid" "blue") 10 53 BACKG)); 63 | 64 | (define (show x) 65 | (cond 66 | [(string? x) 67 | (place-image ROCKET 10 (- HEIGHT CENTER 0) BACKG)] 68 | [(<= -3 x -1) 69 | (place-image (text (number->string x) 20 "red") 70 | 10 (* 3/4 WIDTH) 71 | (place-image ROCKET 72 | 10 (- HEIGHT CENTER 0) 73 | BACKG))] 74 | [(>= x 0) 75 | (place-image ROCKET 10 (- HEIGHT CENTER x) BACKG)])) 76 | 77 | 78 | ; LRCD KeyEvent -> LRCD 79 | ; starts the count-down when space bar is pressed, 80 | ; if the rocket is still resting 81 | (define (launch x ke) 82 | x) 83 | 84 | ; LRCD -> LRCD 85 | ; raises the rocket by YDELTA, 86 | ; if it is moving already 87 | (define (fly x) 88 | x) 89 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/01_From_Positions to posn_Structures.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname |01_From_Positions to posn_Structures|) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 5 Adding Structure 5 | ;; 5.1 From Positions to posn Structures 6 | 7 | (require 2htdp/image) 8 | 9 | ;; A Position on a world canvas is defined by two pieces of data 10 | ;; x-coordinate - the distance from the left margin 11 | ;; y-coordinate - the distance from the top margin 12 | 13 | (make-posn 3 4) 14 | 15 | (define one-posn (make-posn 8 6)) 16 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/02_Computing_with_posns.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 02_Computing_with_posns) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 5 Adding Structure 5 | ;; 5.2 Computing with posns 6 | 7 | ;; Number Number -> Posn 8 | (make-posn 3 4) 9 | 10 | 11 | ;; Selecting components of a posn 12 | 13 | (define p (make-posn 31 26)) 14 | 15 | (posn-x p) ; 31 16 | 17 | (posn-y p) ; 26 18 | 19 | ;; posn identities 20 | 21 | ; (posn-x (make-posn x0 y0)) == x0 22 | ; (posn-y (make-posn x0 y0)) == y0 23 | 24 | ;; evaluation 25 | 26 | (posn-x p) 27 | ; racket replaces p with (make-posn 31 26) 28 | (posn-x (make-posn 31 26)) 29 | 31 30 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/03_Programming_with_posn.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 03_Programming_with_posn) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 5 Adding Structure 5 | ;; 5.3 Programming with posn 6 | ;; Exercises: 63, 64 7 | 8 | 9 | ;; Now consider designing a function that computes the distance of some 10 | ;; location to the origin of the canvas: 11 | 12 | ; computes the distance of ap to the origin 13 | 14 | (check-expect (distance-to-0 (make-posn 0 0)) 0) ; of course 15 | (check-expect (distance-to-0 (make-posn 0 5)) 5) 16 | (check-expect (distance-to-0 (make-posn 7 0)) 7) 17 | 18 | (check-expect (distance-to-0 (make-posn 3 4)) 5) 19 | (check-expect (distance-to-0 (make-posn 8 6)) 10) 20 | (check-expect (distance-to-0 (make-posn 5 12)) 13) 21 | 22 | ;(define (distance-to-0 ap) ; stub 23 | ; 0) 24 | 25 | #; ; template 26 | (define (fn-for-posn ap) 27 | (... (posn-x ap) ... 28 | ... (posn-y ap) ...)) 29 | 30 | (define (distance-to-0 ap) 31 | (sqrt 32 | (+ (sqr (posn-x ap)) 33 | (sqr (posn-y ap))))) 34 | 35 | ;; See exercise 63 - evaluate posn expressions 36 | 37 | ;; See exercise 64 - Manhatten distance 38 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/04_Defining_Structure_Types.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 04_Defining_Structure_Types) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 5 Adding Structure 5 | ;; 5.4 Defining Structure Types 6 | ;; Exercises: 65-68 7 | 8 | 9 | ;(define-struct posn [x y]) 10 | 11 | ;; (define-struct StructuteName [FieldName ..]) 12 | 13 | ;; Structure type definitions defines several function. 14 | 15 | ;; One constructor that creates structure instances 16 | ;; One selector per field 17 | ;; A predicate 18 | 19 | 20 | ;; a structure type definition that we might use to keep track of contacts such 21 | ;; as those in your cell phone: 22 | 23 | (define-struct entry [name phone email]) 24 | 25 | 26 | ;; predifined functions: 27 | 28 | ;; make-entry String String String -> Entry 29 | 30 | ;; entry-name : Entry -> String 31 | ;; entry-phone : Entry -> String 32 | ;; entry-email : Entry -> String 33 | 34 | ;; entry? : Any -> Bool 35 | 36 | 37 | 38 | (make-entry "Sara Lee" "666-7771" "lee@camlu.edu") 39 | 40 | 41 | ;; Sample Problem: 42 | ;; Develop a structure type definition for a program that deals with “bouncing 43 | ;; balls” , briefly mentioned at the very beginning of this chapter. The ball’s 44 | ;; location is a single number, namely the distance of pixels from the top. 45 | ;; Its constant speed is the number of pixels it moves per clock tick. Its 46 | ;; velocity is the speed plus the direction in which it moves. 47 | 48 | ; velocity is a number 49 | ; positive number means ball moves down. 50 | ; negative number means ball moves up. 51 | 52 | (define-struct ball [location velocity]) 53 | 54 | 55 | ;; See exercise 67 - another way to represent bouncing balls: 56 | 57 | ;; a ball with velocity in a 2d universe 58 | 59 | (define-struct vel [deltax deltay]) 60 | ;; "delta" usually indicates a change in a physical quantity over time 61 | 62 | ;; make a ball with a position and a velocity 63 | 64 | (define ball1 65 | (make-ball (make-posn 30 40) (make-vel -10 5))) 66 | 67 | ;; See exercise 68. - Alternative definition. 68 | 69 | 70 | ;;; another example of a nested structure 71 | 72 | (define-struct centry [name home office cell]) 73 | 74 | (define-struct phone [area number]) 75 | 76 | (make-centry "Shriram Fisler" 77 | (make-phone 207 "363-2421") 78 | (make-phone 101 "776-1099") 79 | (make-phone 208 "112-9981")) -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/05_Computing_with_Structures.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 05_Computing_with_Structures) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 5 Adding Structure 5 | ;; 5.5 Computing with Stuctures 6 | ;; Exercises: 69-71 7 | 8 | ;; structues generalize the concept of cartesian points, so computing with 9 | ;; sructs generalizes computing with cartesians 10 | 11 | (define-struct entry [name phone email]) 12 | 13 | (define pl 14 | (make-entry "Al Abe" "666-7771" "lee@x.me")) 15 | 16 | (make-entry "Tara Harp" "666-7770" "th@smlu.edu") 17 | 18 | 19 | ;; See exercise 69 - diagram structs 20 | 21 | 22 | ;; A selector is like a key 23 | 24 | (entry-name pl) ; "Al Abe" 25 | 26 | (define-struct ball [location velocity]) 27 | (define-struct vel [deltax deltay]) 28 | 29 | (define ball1 30 | (make-ball (make-posn 30 40) (make-vel -10 5))) 31 | 32 | (ball-velocity ball1) ; (make-vel -10 5) 33 | 34 | ;; two layers 35 | (vel-deltax (ball-velocity ball1)) ; -10 36 | 37 | ;; A nested definition 38 | ; (define-struct ball [location velocity]) 39 | 40 | (define-struct centry [name home office cell]) 41 | 42 | (define-struct phone [area number]) 43 | 44 | ;; creating a new stucture add new laws to DrRackets environment 45 | ;; creating the struct: 46 | ;(define-struct ball [location velocity]) 47 | ; creates tw laws, one per selector 48 | ;; (ball-location (make-ball l0 v0)) == l0 49 | ;; (ball-velocity (make-ball l0 v0)) == v0 50 | 51 | ;; See exercise 70 - struct laws 52 | 53 | ;; law of centry-office 54 | (phone-area (make-phone 101 "776-1099")) 55 | 56 | ;; law of phone-area 57 | 101 58 | 59 | 60 | ;;; predicates 61 | 62 | (define ap (make-posn 7 0)) 63 | 64 | ;(define pl ; defined above 65 | ; (make-entry "Sara Lee" "666-7771" "lee@camlu.edu")) 66 | 67 | (posn? ap) ;#true 68 | (posn? 42) ;#false 69 | (posn? #true) ;#false 70 | (posn? (make-posn 3 4)) ;#true 71 | 72 | (entry? pl) ;#true 73 | (entry? 42) ;#false 74 | (entry? #true) ;#false 75 | 76 | ;; See exercise 71 77 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/07_Universe_of_Data.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 07_Universe_of_Data) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 5 Adding Structure 5 | ;; 5.7 The Universe of data 6 | ;; Exercises: 76-79 7 | 8 | ;; Strucs expand the universe of BSL types 9 | 10 | (define-struct ball [location velocity]) 11 | 12 | (make-ball -1 0) 13 | (make-ball -1 1) 14 | (make-ball -1 2) 15 | 16 | ;; data definition for Posn 17 | ; Posn is (make-posn Number Number) 18 | 19 | (make-posn (make-posn 1 1) "hello") ; nonsensical 20 | 21 | ;; See exercises 76, 77, 78 22 | 23 | 24 | ;; Provide data example of data definitions to make the data type clear. 25 | 26 | ;; for a built-in collection of data (number, string, Boolean, images), 27 | ;; choose your favorite examples 28 | 29 | ;; for an enumeration, use several of the items of the enumeration; 30 | 31 | ;; for intervals, use the end points (if they are included) and at least one interior point; 32 | 33 | ;; for itemizations, deal with each part separatel 34 | 35 | ;; for data definitions for structures, follow the natural language description; 36 | ;; that is, use the constructor and pick an example from the data collection named for each fiel 37 | 38 | ;; See exetcise 79 39 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/08_Designing_with_Structures.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 08_Designing_with_Structures) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 5 Adding Structure 5 | ;; 5.2 Designing with Structures 6 | ;; Exercises: 80-82 7 | 8 | ;; The design recipe including structures: 9 | 10 | ;; Sample Problem: 11 | ;; Design a function that computes the distance of objects in a 3-dimensional 12 | ;; space to the origin. 13 | 14 | ;; 1. Data design 15 | ;; pieces of data that go to gether belong in a structure 16 | 17 | (define-struct r3 [x y z]) 18 | ; A R3 is a structure: 19 | ; (make-r3 Number Number Number) 20 | 21 | (define ex1 (make-r3 1 2 13)) 22 | (define ex2 (make-r3 -1 0 3)) 23 | 24 | ; R3 -> ??? 25 | #;; template for R3 26 | (define (fn-for-r3 r) 27 | (... (r3-x r) 28 | (r3-y r) 29 | (r3-z r))) 30 | 31 | 32 | ;; 2. Signature, purpose statement and function header: 33 | 34 | ; R3 -> Numbers 35 | ; produces the distance from an T3 to origin 36 | ; origin: (make-r3 0 0 0) 37 | 38 | ;(define (r3-distance-to-0 r) 0) 39 | 40 | ;; 3. Use the examples to creat tests 41 | (check-within (r3-distance-to-0 (make-r3 0 0 0)) 0 0.01) 42 | (check-within (inexact->exact (r3-distance-to-0 ex1)) 13.19 0.01) 43 | (check-within (inexact->exact (r3-distance-to-0 ex2)) 3.162 0.01) 44 | 45 | ;; 4. create a template 46 | #;; 47 | (define (r3-distance-to-0 r) 48 | (... (... (r3-x r)) ; Number 49 | (... (r3-y r)) ; Number 50 | (... (r3-z r)))) ; Number 51 | 52 | ;; 5. Use the selector expressions from the template 53 | 54 | (define (r3-distance-to-0 r) 55 | (sqrt (+ (sqr (r3-x r)) ; Number 56 | (sqr (r3-y r)) ; Number 57 | (sqr (r3-z r))))) 58 | 59 | ;; 6. Test! 60 | 61 | ;; See exercise 80, 81, 82 62 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/09_Structure_in_the_World.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 09_Structure_in_the_World) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | 1`;; HtDP 2e - 5 Adding Structure 5 | ;; 5.9 Structure in the World 6 | 7 | (require 2htdp/image) 8 | (require 2htdp/universe) 9 | 10 | 11 | ;; 5.9 Stucture in the World 12 | 13 | ;; for world programs use stuctures to hold more than one piece of changing 14 | ;; data 15 | 16 | 17 | ;; data type to hold tank and ufo info. 18 | 19 | (define-struct space-game [ufo tank]) 20 | ; space-game is a (make-space-game Number Number) 21 | ; interp. a game state with ufo at y pos and a tank at x pos 22 | 23 | (define GAME-STATE-1 (make-space-game 100 0)) 24 | 25 | ; Space-game -> ??? 26 | #;; template 27 | (define (fn-for-space-game g) 28 | (... (... (space-game-ufo g)) 29 | (... (space-game-tank g)))) 30 | 31 | ; A SpaceGame is a structure: 32 | ; (make-space-game Posn Number). 33 | ; interpretation (make-space-game (make-posn ux uy) tx) 34 | ; describes a configuration where the UFO is 35 | ; at (ux,uy) and the tank's x-coordinate is tx 36 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/10_A_Graphical_Editor.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 10_A_Graphical_Editor) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 5 Adding Structure 5 | ;; 5.10 A Graphical Editor 6 | ;; Exercises: 83-87 7 | 8 | 9 | ;; 5.10 A Graphical Editor 10 | 11 | ;; An editor needs to keep track of two items: the text entered so far, and 12 | ;; the cursor positon. 13 | 14 | 15 | (define-struct editor [pre post]) 16 | ; An Editor is a structure: 17 | ; (make-editor String String) 18 | ; interpretation (make-editor s t) describes an editor 19 | ; whose visible text is (string-append s t) with 20 | ; the cursor displayed between s and t 21 | 22 | (define ED1 (make-editor "" "")) ; Empty editor 23 | (define ED2 (make-editor "" "Hello world")) ; Cursor at beginning or buffuer 24 | (define ED3 (make-editor "He" "llo world")) ; Cursor after "e" in buffer 25 | 26 | ; Editor -> ??? 27 | #;; template 28 | (define (fn-for-editor e) 29 | (... (... (editor-pre e)) 30 | (... (editor-post e)))) 31 | 32 | 33 | ;; See exercise 83, 84, 85, 86, 87 34 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/11_More_Virtual_Pets.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 11_More_Virtual_Pets) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 5 Adding Structure 5 | ;; 5.11 More Virtual Pets 6 | ;; Exercises: 88-93 7 | 8 | ;; see exercises 88, 89, 90, 91 9 | 10 | (require 2htdp/image) 11 | (require 2htdp/universe) 12 | 13 | (define RED "red") 14 | 15 | (define CHAM (bitmap "images/chameleon.png")) 16 | 17 | (define background 18 | (rectangle (image-width CHAM) 19 | (image-height CHAM) 20 | "solid" 21 | RED)) 22 | 23 | (overlay CHAM background) 24 | 25 | 26 | ;; See exercise 92. 27 | ;; Ex. 92: 28 | ;; Design the cham program, which has the chameleon continuously walking 29 | ;; across the canvas, from left to right. When it reaches the right end of the 30 | ;; canvas, it disappears and immediately reappears on the left. Like the cat, 31 | ;; the chameleon gets hungry from all the walking and, as time passes by, this 32 | ;; hunger expresses itself as unhappiness. 33 | ;; See chameleon1.rkt 34 | 35 | ;; Ex. 93: 36 | ;; Copy your solution to exercise 92 and modify the copy so that the chameleon 37 | ;; walks across a tricolor background. Our solution uses these colors 38 | 39 | ;(define BACKGROUND 40 | ; (beside (empty-scene WIDTH HEIGHT "green") 41 | ; (empty-scene WIDTH HEIGHT "white") 42 | ; (empty-scene WIDTH HEIGHT "red"))) 43 | 44 | ;; but you may use any colors. Observe how the chameleon changes colors to 45 | ;; blend in as it crosses the border between two colors. 46 | 47 | ;; Note: 48 | ;; When you watch the animation carefully, you see the chameleon riding on a 49 | ;; white rectangle. If you know how to use image editing software, modify the 50 | ;; picture so that the white rectangle is invisible. Then the chameleon will 51 | ;; really blend in. 52 | ;; See chameleon2.rkt -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/ex063.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex063) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex 63. 5 | ;; Evaluate the following expressions: 6 | 7 | ;;(distance-to-0 (make-posn 3 4)) 8 | 9 | ;;(distance-to-0 (make-posn 6 (* 2 4))) 10 | 11 | ;;(+ (distance-to-0 (make-posn 12 5)) 10) 12 | 13 | (define (distance-to-0 ap) 14 | (sqrt 15 | (+ (sqr (posn-x ap)) 16 | (sqr (posn-y ap))))) 17 | 18 | ;;;;;; 19 | 20 | ;; 1. 21 | (distance-to-0 (make-posn 3 4)) 22 | 23 | ;#| 24 | (sqrt 25 | (+ (sqr (posn-x (make-posn 3 4))) 26 | (sqr (posn-y (make-posn 3 4))))) 27 | 28 | (sqrt 29 | (+ (sqr 3) 30 | (sqr (posn-y (make-posn 3 4))))) 31 | 32 | (sqrt 33 | (+ 9 34 | (sqr (posn-y (make-posn 3 4))))) 35 | 36 | (sqrt 37 | (+ 9 38 | (sqr 4))) 39 | 40 | (sqrt 41 | (+ 9 42 | 16)) 43 | 44 | (sqrt 25) 45 | 46 | 5 47 | ;|# 48 | 49 | 50 | ;; 2. 51 | (distance-to-0 (make-posn 6 (* 2 4))) 52 | 53 | ;#| 54 | (distance-to-0 (make-posn 6 8)) 55 | 56 | (sqrt 57 | (+ (sqr (posn-x (make-posn 6 8))) 58 | (sqr (posn-y (make-posn 6 8))))) 59 | 60 | (sqrt 61 | (+ (sqr 6) 62 | (sqr (posn-y (make-posn 6 8))))) 63 | 64 | (sqrt 65 | (+ 36 66 | (sqr (posn-y (make-posn 6 8))))) 67 | 68 | (sqrt 69 | (+ 36 70 | (sqr 8))) 71 | 72 | (sqrt 73 | (+ 36 74 | 64)) 75 | 76 | (sqrt 100) 77 | ;; == 10 78 | ;|# 79 | 80 | 81 | ;; 3. 82 | (+ (distance-to-0 (make-posn 12 5)) 10) 83 | 84 | ;#| 85 | 86 | (+ 87 | (sqrt 88 | (+ (sqr (posn-x (make-posn 12 5))) 89 | (sqr (posn-y (make-posn 12 5))))) 90 | 10) 91 | 92 | (+ 93 | (sqrt 94 | (+ (sqr 12) 95 | (sqr (posn-y (make-posn 12 5))))) 96 | 10) 97 | 98 | (+ 99 | (sqrt 100 | (+ 144 101 | (sqr (posn-y (make-posn 12 5))))) 102 | 10) 103 | 104 | (+ 105 | (sqrt 106 | (+ 144 107 | (sqr 5))) 108 | 10) 109 | 110 | (+ 111 | (sqrt 112 | (+ 144 113 | 25)) 114 | 10) 115 | 116 | (+ 117 | (sqrt 169) 118 | 10) 119 | 120 | (+ 121 | 13 122 | 10) 123 | 124 | 23 125 | ;|# 126 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/ex064.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex064) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 64: 5 | ;; Design the function manhattan-distance, which measures the Manhattan 6 | ;; distance of the given posn to the origin. 7 | 8 | ; Posn -> Number 9 | ; produce the 'Manhattan distance' from a given Posn to the Origin 10 | (check-expect (manhattan-distance (make-posn 0 0)) 0) 11 | (check-expect (manhattan-distance (make-posn 5 0)) 5) 12 | (check-expect (manhattan-distance (make-posn 0 4)) 4) 13 | (check-expect (manhattan-distance (make-posn 5 4)) 9) 14 | 15 | ;(define (manhattan-distance p) 0) ; stub 16 | 17 | (define (manhattan-distance p) 18 | (+ (posn-x p) 19 | (posn-y p))) 20 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/ex065.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex065) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex 65. 5 | ;; Write down the names of the functions 6 | ;; (constructors, selectors, and predicates) that it introduces 7 | 8 | (define-struct movie [title producer year]) 9 | ;; make-movie, movie?, movie-title, movie-producer, movie-year 10 | 11 | (define-struct person [name hair eyes phone]) 12 | ;; make-person, person?, person-name, person-hair, person-eyes 13 | 14 | (define-struct pet [name number]) 15 | ;; make-pet, pet? pet-name pet-number 16 | 17 | (define-struct CD [artist title price]) 18 | ;; make-CD, CD?, CD-artist, CD-title, CD-price 19 | 20 | (define-struct sweater [material size producer]) 21 | ;; make-sweater, seater? seater-material, seater-size, seater-produce 22 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/ex066.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex066) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 66: 5 | ;; Revisit the structure type definitions of exercise 65. Make sensible 6 | ;; guesses as to what kind of values go with which fields. Then create at 7 | ;; least one instance per structure type definition. 8 | 9 | (define-struct movie [title producer year]) 10 | ; title: String 11 | ; producer: String 12 | ; year: PositiveNumber 13 | (make-movie "The Great Gatsby" "John Ford" 1955) 14 | 15 | (define-struct person [name hair eyes phone]) 16 | ; name: String 17 | ; hair: String 18 | ; eyes: String 19 | ; phone: String 20 | (make-person "Susanne" "Blonde" "Blue" "555-3545") 21 | 22 | (define-struct pet [name number]) 23 | ; name: String 24 | ; number: PositiveNumber 25 | (make-pet "Muffy" 5) 26 | 27 | (define-struct CD [artist title price]) 28 | ; artist: String 29 | ; title: String 30 | ; price Number 31 | (make-CD "Joan Jett" "Raw" 13.99) 32 | 33 | (define-struct sweater [material size producer]) 34 | ; material: String 35 | ; size: Natrural 36 | ; produce: String 37 | (make-sweater "Cashmere" 10 "Levies") 38 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/ex067.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex067) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex 67 5 | ;; Here is another way to represent bouncing balls" 6 | 7 | ;; location is a number 8 | ;; interpretation. number of pixes distance of balld from top. 9 | 10 | ;; direction is one of two Strings 11 | ;; - "up" 12 | ;; - "down 13 | ;; interpretation. Direction of balld movement. 14 | 15 | (define SPEED 3) 16 | (define-struct balld [location direction]) 17 | (make-balld 10 "up") 18 | 19 | ;; Interpret this code fragment and create other instances of balld 20 | 21 | (make-balld 100 "up") 22 | (make-balld (+ 25 SPEED) "down") 23 | (make-balld 0 "up") 24 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/ex068.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex068) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 68: 5 | ;; An alternative to the nested data representation of balls uses four fields 6 | ;; to keep track of the four properties: 7 | 8 | 9 | ;(define ball1 10 | ; (make-ball (make-posn 30 40) (make-vel -10 5))) 11 | 12 | (define-struct ballf [x y deltax deltay]) 13 | 14 | ;; Programmers call this a flat representation. 15 | ;; Create an instance of ballf that has the same interpretation as ball1. 16 | 17 | (define ball2 18 | (make-ballf 30 40 -10 5)) 19 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/ex069.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex069) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 69: 5 | ;; Draw box representations for the solution of exercise 65. 6 | 7 | (define-struct movie [title producer year]) 8 | (make-movie "The Great Gatsby" "John Ford" 1955) 9 | 10 | (define-struct person [name hair eyes phone]) 11 | (make-person "Susanne" "Blonde" "Blue" "555-3545") 12 | 13 | (define-struct pet [name number]) 14 | (make-pet "Muffy" 5) 15 | 16 | (define-struct CD [artist title price]) 17 | (define-struct sweater [material size producer]) 18 | 19 | (define-struct sweater [material size producer]) 20 | (make-sweater "Cashmere" 10 "Levies") 21 | 22 | ;; you will just have to assume that I did this. :-) 23 | 24 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/ex070.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex070) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Exercise 70. Spell out the laws for these structure type definitions: 5 | 6 | 7 | (define-struct centry [name home office cell]) 8 | ; (cntry-name (make-centy n0 h0 o0 c0)) == n0 9 | ; (cntry-home (make-centy n0 h0 o0 c0)) == h0 10 | ; (cntry-office (make-centy n0 h0 o0 c0)) == 00 11 | ; (cntry-cell (make-centy n0 h0 o0 c0)) == c0 12 | 13 | (define-struct phone [area number]) 14 | ; (phone-area (make-phone a0 no)) == a0 15 | ; (phone-number (make-phone a0 n0)) == n0 16 | 17 | ;; Use these laws to explain how DrRacket finds 101 as the value of 18 | (phone-area 19 | (centry-office 20 | (make-centry 21 | "Shriram Fisler" 22 | (make-phone 207 "363-2421") 23 | (make-phone 101 "776-1099") 24 | (make-phone 208 "112-9981")))) 25 | 26 | (phone-area (make-phone 101 "776-1099")) 27 | 28 | 101 29 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/ex071.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex071) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 71: 5 | ;; Place the following into DrRacket’s definition area: 6 | 7 | ; distances in terms of pixels: 8 | (define HEIGHT 200) 9 | (define MIDDLE (quotient HEIGHT 2)) ; 100 10 | (define WIDTH 400) 11 | (define CENTER (quotient WIDTH 2)) ; 200 12 | 13 | (define-struct game [left-player right-player ball]) 14 | 15 | (define game0 16 | (make-game MIDDLE MIDDLE (make-posn CENTER CENTER))) 17 | 18 | ;; Explain the results with step-by-step computations. 19 | ;; Double-check your computations with DrRacket’s stepper. 20 | 21 | (game-ball game0) 22 | ;(game-ball (make-game MIDDLE MIDDLE (make-posn CENTER CENTER))) 23 | ;(game-ball (make-game 100 MIDDLE (make-posn CENTER CENTER))) 24 | ;(game-ball (make-game 100 100 (make-posn CENTER CENTER))) 25 | ;(game-ball (make-game 100 100 (make-posn 200 CENTER))) 26 | ;(game-ball (make-game 100 100 (make-posn 200 200))) 27 | ;(make-posn 200 200) 28 | 29 | (posn? (game-ball game0)) 30 | ;(posn? (game-ball (make-game MIDDLE MIDDLE (make-posn CENTER CENTER)))) 31 | ;(posn? (game-ball (make-game 100 MIDDLE (make-posn CENTER CENTER)))) 32 | ;(posn? (game-ball (make-game 100 100 (make-posn CENTER CENTER)))) 33 | ;(posn? (game-ball (make-game 100 100 (make-posn 200 CENTER)))) 34 | ;(posn? (game-ball (make-game 100 100 (make-posn 200 200)))) 35 | ;(posn? (make-posn 200 200)) 36 | ;#true 37 | 38 | (game-left-player game0) 39 | ;(game-left-player (make-game MIDDLE MIDDLE (make-posn CENTER CENTER))) 40 | ;(game-left-player (make-game 100 MIDDLE (make-posn CENTER CENTER))) 41 | ;(game-left-player (make-game 100 100 (make-posn CENTER CENTER))) 42 | ;(game-left-player (make-game 100 100 (make-posn 200 CENTER))) 43 | ;(game-left-player (make-game 100 100 (make-posn 200 200))) 44 | ;100 -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/ex072.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex072) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 72: 5 | ;; Formulate a data definition for the above phone structure type 6 | ;; definition that accommodates the given examples. 7 | 8 | (define-struct phone [area number]) 9 | ; A Phone is a structure: 10 | ; (make-phone Number String) 11 | ; interpretation 12 | ; area the area code 13 | ; number the local phone-number 14 | 15 | (make-phone 207 "363-2421") 16 | 17 | ;; Next formulate a data definition for phone numbers using this 18 | ;; structure type definition: 19 | ;; Describe the content of the three fields 20 | ;; as precisely as possible with intervals. 21 | 22 | (define-struct phone# [area switch num]) 23 | ; A Phone# is a structure: 24 | ; (make-phone# Number Number Number) 25 | ; interpretation: 26 | ; area is the area code [100, 999] 27 | ; switch is phonew switch exhange [100, 999] 28 | ; number is number in the neighborhood [0000, 9999] 29 | 30 | (make-phone# 207 363 2421) 31 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/ex073.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex073) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 73: 5 | ;; Design the function posn-up-x, which consumes a Posn p and a Number n. 6 | ;; It produces a Posn like p with n in the x field. 7 | 8 | ;; Note Functions such as posn-up-x are often called updaters or functional 9 | ;; setters. They are extremely useful when you write large programs. 10 | 11 | ; Posn Number -> Posn 12 | ; produces a Posn given a Posn and a Number with the x field replaced by n 13 | (check-expect (posn-up-x (make-posn 10 20) 30) 14 | (make-posn 30 20)) 15 | 16 | ; (define (posn-up-x p n) (make-posn 0 0)) ; stub 17 | 18 | (define (posn-up-x p n) 19 | (make-posn n (posn-y p))) 20 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/ex074.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname reddot1) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 5 Adding Structure 5 | ;; 5.6 Programming with Structures 6 | ;; Exercise: 74 7 | 8 | ;; Ex. 74: 9 | ;; Copy all relevant constant and function definitions to DrRacket’s definition 10 | ;; area. Add the tests and make sure they pass. 11 | ;; Then run the program and use the mouse to place the red dot. 12 | 13 | 14 | (require 2htdp/image) 15 | (require 2htdp/universe) 16 | 17 | (define MTS (empty-scene 100 100)) 18 | (define DOT (circle 3 "solid" "red")) 19 | 20 | ; A Posn represents the state of the world. 21 | 22 | #; ; template for Posn 23 | (define (fn-for-posn p) 24 | (... (posn-x p) ... (posn-y p) ...)) 25 | 26 | 27 | ; Posn -> Posn 28 | ; run: (main (make-posn 20 10) 29 | 30 | (define (main p0) 31 | (big-bang p0 32 | [on-tick x+] 33 | [on-mouse reset-dot] 34 | [to-draw scene+dot])) 35 | 36 | 37 | ; Posn -> Image 38 | ; adds a red spot to MTS at p 39 | (check-expect (scene+dot (make-posn 10 20)) 40 | (place-image DOT 10 20 MTS)) 41 | (check-expect (scene+dot (make-posn 88 73)) 42 | (place-image DOT 88 73 MTS)) 43 | ;(define (scene+dot p) MTS) ; stub 44 | 45 | (define (scene+dot p) 46 | (place-image DOT (posn-x p) (posn-y p) MTS)) 47 | 48 | 49 | ; Posn -> Posn 50 | ; increases the x-coordinate of p by 3 51 | (check-expect (x+ (make-posn 10 20)) 52 | (make-posn 13 20)) 53 | ;(define (x+ p) p) ; stub 54 | 55 | (define (x+ p) 56 | (make-posn (+ (posn-x p) 3) (posn-y p))) 57 | 58 | 59 | ; Posn Number Number MouseEvt -> Posn 60 | ; for mouse clicks, (make-posn x y); otherwise p 61 | (check-expect 62 | (reset-dot (make-posn 10 20) 29 31 "button-down") 63 | (make-posn 29 31)) 64 | (check-expect 65 | (reset-dot (make-posn 10 20) 29 31 "button-up") 66 | (make-posn 10 20)) 67 | ;(define (reset-dot p x y me) p) ; stub 68 | 69 | #; ; template 70 | (define (reset-dot p x y me) 71 | (cond 72 | [(mouse=? "button-down" me) (... p ... x y ...)] 73 | [else (... p ... x y ...)])) 74 | 75 | (define (reset-dot p x y me) 76 | (cond 77 | [(mouse=? "button-down" me) (make-posn x y)] 78 | [else p])) 79 | 80 | ; (main (make-posn 10 30)) 81 | 82 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/ex075.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex075) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 75: 5 | ;; Enter these definitions and their test cases into the definitions area of 6 | ;; DrRacket and make sure they work. It is the first time that we made a “wish” 7 | ;; and you need to make sure you understand how the two functions work together. 8 | 9 | (define-struct vel [deltax deltay]) 10 | ; A Vel is a structure: 11 | ; (make-vel Number Number) 12 | ; interpretation (make-vel dx dy) means a velocity of 13 | ; dx pixels [per tick] along the horizontal and 14 | ; dy pixels [per tick] along the vertical direction 15 | 16 | #; ; template 17 | (define (fun-for-vel v) 18 | (... (deltax v) ... (deltay v))) 19 | 20 | 21 | (define-struct ufo [loc vel]) 22 | ; A UFO is a structure: 23 | ; (make-ufo Posn Vel) 24 | ; interpretation (make-ufo p v) is at location 25 | ; p moving at velocity v. 26 | 27 | #;; template 28 | ; UFO -> ??? 29 | (define (fun-for-ufo u) 30 | (... (posn-x (ufo-loc u)) ... 31 | ... (posn-y (ufo-loc u)) ... 32 | ... (vel-deltax (ufo-vel u)) ... 33 | ... (vel-deltay (ufo-vel u)) ...)) 34 | 35 | ;; some UFO examples: 36 | (define v1 (make-vel 8 -3)) 37 | (define v2 (make-vel -5 -3)) 38 | 39 | (define p1 (make-posn 22 80)) 40 | (define p2 (make-posn 30 77)) 41 | 42 | (define u1 (make-ufo p1 v1)) 43 | (define u2 (make-ufo p1 v2)) 44 | (define u3 (make-ufo p2 v1)) 45 | (define u4 (make-ufo p2 v2)) 46 | 47 | 48 | ; UFO -> UFO 49 | ; determins where u move in one clock tick; 50 | ; leaves the velocity as is 51 | 52 | (check-expect (ufo-move-1 u1) u3) 53 | (check-expect (ufo-move-1 u2) 54 | (make-ufo (make-posn 17 77) v2)) 55 | 56 | ;(define (ufo-move-1 u) u) ; stub 57 | 58 | (define (ufo-move-1 u) 59 | (make-ufo (posn+ (ufo-loc u) (ufo-vel u)) 60 | (ufo-vel u))) 61 | 62 | 63 | ; Posn Vel -> Posn 64 | ; adds v to p 65 | (check-expect (posn+ p1 v1) p2) 66 | (check-expect (posn+ p1 v2) (make-posn 17 77)) 67 | 68 | ;(define (posn+ p v) p) ; stub 69 | 70 | #;; template 71 | (define (posn+ p v) 72 | (... (posn-x p) ... (posn-y p) ... 73 | ... (vel-deltax v) ... (vel-deltay v) ...)) 74 | 75 | (define (posn+ p v) 76 | (make-posn (+ (posn-x p) (vel-deltax v)) 77 | (+ (posn-y p) (vel-deltay v)))) 78 | 79 | ;; test 80 | ;(ufo-move-1 u1) 81 | ;(ufo-move-1 u2) 82 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/ex076.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex76) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 76: 5 | ;; Formulate data definitions for the following structure type definitions: 6 | 7 | ; Note: I include a function templare and an example as part of a data definition. 8 | 9 | (define-struct movie [title producer year]) 10 | ; movie is (make-movie String String Number) 11 | ; interp: title is the name of the movie 12 | ; producer is the name of the producer 13 | ; year is the year the movie was made 14 | 15 | (define MOVIE1 (make-movie "Gone With the Wind" "John Ford" 1944)) 16 | 17 | #;; template 18 | (define (fn-for-movie m) 19 | (... (movie-title m) 20 | (movie-producer m) 21 | (movie-year m))) 22 | 23 | 24 | (define-struct person1 [name hair eyes phone]) 25 | ; person is a (make-person1 String String String String) 26 | ; interp: a person1 is composed of name, hair color, eye color, phone number 27 | 28 | (define PERSON1 (make-person1 "Bettsy Sue" "Brown" "Brown" "555-2223")) 29 | 30 | #;; template 31 | (define (fn-for-person1 p) 32 | (... (person1-name p) 33 | (person1-hair p) 34 | (person1-eyes p) 35 | (person1-phone))) 36 | 37 | 38 | (define-struct pet [name number]) 39 | ; pet is a (make-pet String Number) 40 | ; interp: a pet with a name and an id number 41 | 42 | (define CAT1 (make-pet "Fluffy" 100)) 43 | 44 | #;; template 45 | (define (fn-for-pet p) 46 | (... (pet-name p) 47 | (pet-number p))) 48 | 49 | 50 | (define-struct CD [artist title price]) 51 | ; CD is a (make-CD String String Number) 52 | ; interp: a CD with an artist name, a CD title and a price in dollars 53 | 54 | (define CD1 (make-CD "Beethoven" "Beethoven's Piano Concertos" 10.44)) 55 | 56 | #;; template 57 | (define (fn-for-CD cd) 58 | (... (CD-artist cd) 59 | (CD-title cd) 60 | (CD-price cd))) 61 | 62 | 63 | (define-struct sweater [material size producer]) 64 | ; sweater is a (make-sweater String String String) 65 | ; interp: a sweater has a material, a size, and a manufacturer 66 | 67 | (define SWEATER1 (make-sweater "angora" "large" "Archer Daniels Midland")) 68 | 69 | #;; template 70 | (define (fn-for-sweater s) 71 | (... (sweater-material s) 72 | (sweater-size s) 73 | (sweater-producer s))) 74 | 75 | 76 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/ex077.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex77) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ; Ex. 77: 5 | ;; Provide a structure type definition and a data definition for representing 6 | ;; points in time since midnight. 7 | ;; A point in time consists of three numbers: hours, minutes, and seconds 8 | 9 | (define-struct time-pt [hours minutes seconds]) 10 | ; time-pt is a (make-time-pt Number Number Number)) 11 | ; interp: a point in time having hours, minutes, seconds 12 | 13 | (define TIME1 (make-time-pt 10 30 00)) ; 10:30:00 14 | 15 | #;; template 16 | (define (fn-for-time-pt tm) 17 | (... (time-pt-hours tm) 18 | (time-pt-minutes tm) 19 | (time-pt-seconds tm))) 20 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/ex078.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex078) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 78: 5 | ;; Provide a structure type definition and a data definition for representing 6 | ;; three-letter words. A word consists of lower-case letters, represented with 7 | ;; the one-letter strings "a" through "z" plus #false. 8 | ;; Note This exercise is a part of the design of a Hangman game; 9 | ;; see exercise 396. 10 | 11 | ;; LCL is one of 12 | ; 1String [a,z] 13 | ; #false 14 | ; interp. lower case letters [a,z] or no letter 15 | 16 | (define L1 "a") 17 | (define L2 "k") 18 | (define L3 #false) 19 | 20 | #;; template 21 | (define (fn-for-LCL l) 22 | (cond [(and (string? l) (string<=? "a" l) (string<=? l "z")) ...] 23 | [(equal? #false l) ...])) 24 | 25 | 26 | (define-struct word3 [let1 let2 let3]) 27 | ;; word3 is a (make-word3 LCL LCL LCL) 28 | ;; interp. word3 consists of 3 lower case letters 29 | 30 | (define WORD1 (make-word3 "c" "a" "t")) 31 | (define WORD2 (make-word3 "d" "o" "g")) 32 | (define WORD3 (make-word3 "m" "e" #false)) 33 | 34 | #;; template 35 | (define (fn-for-word3 w) 36 | (... (... (word3-let1 w)) 37 | (... (word3-let2 w)) 38 | (... (word3-let3 w)))) 39 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/ex079.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex079) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 79: 5 | ;; Create examples for the following data definitions: 6 | 7 | ; A Color is one of: 8 | ; — "white" 9 | ; — "yellow" 10 | ; — "orange" 11 | ; — "green" 12 | ; — "red" 13 | ; — "blue" 14 | ; — "black" 15 | 16 | (define RED "red") 17 | (define BLUE "blue") 18 | (define GREEN "green") 19 | 20 | 21 | ; H is a Number between 0 and 100. 22 | ; interpretation represents a “happiness value” 23 | 24 | (define HAPPY 100) 25 | (define MEH 50) 26 | (define SAD 0) 27 | 28 | 29 | (define-struct person [fstname lstname male?]) 30 | ; A Person is a structure: 31 | ; (make-person String String Boolean) 32 | 33 | (define TOM (make-person "Tom" "Smith" #true)) 34 | (define BETTY (make-person "Betty" "Lou" #false)) 35 | 36 | ;; I don't think field names that look like a predicate is a bad idea. 37 | ;; They willl generally be tested with predicates, and the names are just 38 | ;; for documentation 39 | 40 | 41 | (define-struct dog [owner name age happiness]) 42 | ; A Dog is a structure: 43 | ; (make-dog Person String PositiveInteger H) 44 | ; interp. a dog with an owner, the dog's name, the dog's age 45 | ;; and the dog's happiness level` 46 | 47 | (define FEFE (make-dog (make-person "Sue" "Hech" #false) 48 | "Fefe" 3 80)) 49 | (define BUTCH (make-dog (make-person "Bill" "Jander" #true) 50 | "Butch" 8 50)) 51 | 52 | 53 | ; A Weapon is one of: 54 | ; — #false 55 | ; — Posn 56 | ; interpretation #false means the missile hasn't 57 | ; been fired yet; a Posn means it is in flight 58 | 59 | (define MISSLE1 #false) 60 | (define DRONE2 (make-posn 30 56)) 61 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/ex080.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex080) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 80 5 | ;; Create templates for functions that consume instances of the following 6 | ;; structure types: 7 | 8 | (define-struct movie [title director year]) 9 | 10 | ; Movie -> ??? 11 | (define (fn-for-movie m) 12 | (... (movie-title m)) 13 | (... (movie-director m)) 14 | (... (movie-year m))) 15 | 16 | 17 | (define-struct person [name hair eyes phone]) 18 | 19 | ; Person -> ??? 20 | (define (fn-for-person p) 21 | (... (person-name p)) 22 | (... (person-hair p)) 23 | (... (person-eyes p)) 24 | (... (person-phone p))) 25 | 26 | 27 | (define-struct pet [name number]) 28 | 29 | ; Pet -> ??? 30 | (define (fn-for-pet p) 31 | (... (pet-name p)) 32 | (... (pet-number p))) 33 | 34 | 35 | (define-struct CD [artist title price]) 36 | 37 | ; CD -> ??? 38 | (define (fn-for-CD cd) 39 | (... (CD-artist cd)) 40 | (... (CD-title cd)) 41 | (... (CD-price cd))) 42 | 43 | 44 | (define-struct sweater [material size color]) 45 | 46 | ; Sweater -> ??? 47 | (define (fn-for-sweater s) 48 | (... (sweater-material s)) 49 | (... (sweater-size s)) 50 | (... (sweater-color s))) 51 | 52 | 53 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/ex081.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex081) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 81: 5 | ;; Design the function time->seconds, which consumes instances of time 6 | ;; structures and produces the number of seconds that have passed since 7 | ;; midnight. 8 | ;; For example, if you are representing 12 hours, 30 minutes, and 2 seconds 9 | ;; with one of these structures and if you then apply time->seconds to this 10 | ;; instance, the correct result is 45002. 11 | 12 | (define-struct time [hours minutes seconds]) 13 | ; timet is a (make-time Number Number Number)) 14 | ; interp: a point in time having hours, minutes, seconds 15 | 16 | (define TIME1 (make-time 10 30 00)) ; 10:30 17 | 18 | #;; Time -> ??? 19 | (define (fn-for-time t) 20 | (... (... (time-hours t)) 21 | (... (time-minutes t)) 22 | (... (time-seconds t)))) 23 | 24 | 25 | ; Time -> Number 26 | ; convert a given time to seconds 27 | (check-expect (time->seconds (make-time 0 0 0)) 0) 28 | (check-expect (time->seconds (make-time 12 30 2)) 45002) 29 | (check-expect (time->seconds TIME1) 37800) 30 | 31 | ;(define (time->seconds t) 0) ; stub 32 | 33 | (define (time->seconds t) 34 | (+ (* 3600 (time-hours t)) 35 | (* 60 (time-minutes t)) 36 | (time-seconds t))) 37 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/ex082.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex082) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 82: 5 | ;; Design the function compare-word. The function consumes two (representations 6 | ;; of) three-letter words. It produces a word that indicates where the given 7 | ;; ones agree and disagree. The function retains the content of the structure 8 | ;; fields if the two agree; otherwise it places #false in the field of the 9 | ;; resulting word. 10 | ;; Hint The exercises mentions two tasks: the comparison of words and the 11 | ;; comparison of “letters.” 12 | 13 | ;; data definition: 14 | 15 | ;; LCL is one of 16 | ; 1String [a,z] 17 | ; #false 18 | ; interp. lower case letters [a,z] or no letter 19 | 20 | (define L1 "a") 21 | (define L2 "k") 22 | (define L3 #false) 23 | 24 | #;; template 25 | (define (fn-for-LCL l) 26 | (cond [(and (string? l) (string<=? "a" l "z")) ...] 27 | [(equal? #false l) ...])) 28 | 29 | 30 | (define-struct word3 [let1 let2 let3]) 31 | ;; word3 is a (make-word3 LCL LCL LCL) 32 | ;; interp. word3 consists of 3 lower case letters 33 | 34 | (define WORD1 (make-word3 "c" "a" "t")) 35 | (define WORD2 (make-word3 "d" "o" "g")) 36 | (define WORD3 (make-word3 "m" "e" #false)) 37 | 38 | #;; template 39 | (define (fn-for-word3 w) 40 | (... (... (word3-let1 w)) 41 | (... (word3-let2 w)) 42 | (... (word3-let3 w)))) 43 | 44 | ; Word3 World3 -> Word3 45 | ; matches two Word3 letter for letter and returns one that contains the matched 46 | ; letters and #false for unmatched letters 47 | 48 | (check-expect (compare-word (make-word3 "c" "a" "t") (make-word3 "c" "a" "t")) 49 | (make-word3 "c" "a" "t")) 50 | 51 | (check-expect (compare-word (make-word3 "c" "a" "t") (make-word3 "c" "a" "b")) 52 | (make-word3 "c" "a" #false)) 53 | 54 | (check-expect (compare-word (make-word3 "c" "a" "t") (make-word3 "c" "a" #false)) 55 | (make-word3 "c" "a" #false)) 56 | 57 | (check-expect (compare-word (make-word3 "c" "a" "t") (make-word3 "d" "o" "g")) 58 | (make-word3 #false #false #false)) 59 | 60 | ;(define (compare-word w1 w2) (make-word3 "a" "a" "a")) ; stub 61 | 62 | (define (compare-word w1 w2 ) 63 | (make-word3 (compare-letter (word3-let1 w1) (word3-let1 w2)) 64 | (compare-letter (word3-let2 w1) (word3-let2 w2)) 65 | (compare-letter (word3-let3 w1) (word3-let3 w2)))) 66 | 67 | ; LCL LCL -> LCL 68 | ; campare two letters and return them if they match or 69 | ; return #false if they don't 70 | 71 | (check-expect (compare-letter "a" "a") "a") 72 | (check-expect (compare-letter "a" "b") #false) 73 | (check-expect (compare-letter "a" #false) #false) 74 | 75 | ;(define (compare-letter l1 l2) "o") ; stub 76 | 77 | (define (compare-letter l1 l2) 78 | (cond [(and (string? l1) (string? l2)) 79 | (cond [(string=? l1 l2) l1] 80 | [else #false])] 81 | [else #false])) 82 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/ex083.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex083) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 83: 5 | ;; Design the function render, which consumes an Editor and produces an image. 6 | 7 | (require 2htdp/image) 8 | (require 2htdp/universe) 9 | 10 | ;; data definition 11 | 12 | (define-struct editor [pre post]) 13 | ; An Editor is a structure: 14 | ; (make-editor String String) 15 | ; interpretation (make-editor s t) describes an editor 16 | ; whose visible text is (string-append s t) with 17 | ; the cursor displayed between s and t 18 | 19 | (define ED1 (make-editor "" "")) ; Empty editor 20 | (define ED2 (make-editor "" "Hello world")) ; Cursor at beginning or buffuer 21 | (define ED3 (make-editor "He" "llo world")) ; Cursor after "e" in buffer 22 | 23 | 24 | ;; graphical constants 25 | 26 | (define TEXT-SIZE 16) 27 | (define TEXT-COLOR "black") 28 | 29 | (define BUFFER-LENGTH 200) ; length of buffer display 30 | (define BUFFER-HEIGHT 20) ; height of buffer display 31 | 32 | (define MT (empty-scene BUFFER-LENGTH BUFFER-HEIGHT)) 33 | 34 | (define CURSOR-COLOR "red") 35 | 36 | (define CURSOR (rectangle 1 BUFFER-HEIGHT "solid" CURSOR-COLOR)) 37 | 38 | 39 | ; String -> Image 40 | ; Render text with default properties 41 | (check-expect (render-text "") 42 | (text "" TEXT-SIZE TEXT-COLOR)) 43 | (check-expect (render-text "hello") 44 | (text "hello" TEXT-SIZE TEXT-COLOR)) 45 | 46 | (define (render-text txt) 47 | (text txt TEXT-SIZE TEXT-COLOR)) 48 | 49 | 50 | ; try a mock-up 51 | (define MOCK-UP1 (overlay/align "left" "center" 52 | (beside (render-text "hel") 53 | CURSOR 54 | (render-text "lo world")) 55 | MT)) 56 | 57 | 58 | 59 | ; Editor -> Image 60 | ; produce an Image of text given an Editor 61 | 62 | (check-expect (render (make-editor "" "")) 63 | (overlay/align "left" "center" 64 | (beside (render-text "") 65 | CURSOR 66 | (render-text "")) 67 | MT)) 68 | 69 | (check-expect (render (make-editor "hel" "lo world")) 70 | (overlay/align "left" "center" 71 | (beside (render-text "hel") 72 | CURSOR 73 | (render-text "lo world")) 74 | MT)) 75 | 76 | ;(define (render ed) MT) ;stub 77 | 78 | (define (render ed) 79 | (overlay/align "left" "center" 80 | (beside (render-text (editor-pre ed)) 81 | CURSOR 82 | (render-text (editor-post ed))) 83 | MT)) 84 | 85 | ;; test examples 86 | ;(render ED1) 87 | ;(render ED2) 88 | ;(render ED3) 89 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/ex088.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex088) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ; Ex. 88: 5 | ; Define a structure type that keeps track of the cat’s x-coordinate and its 6 | ; happiness. Then formulate a data definition for cats, dubbed VCat, including 7 | ; an interpretation. 8 | 9 | 10 | ; happy cat simulation 11 | 12 | (define-struct vcat [x-pos happy]) 13 | ; cat is a (make-vcat Number Number[0,100]) 14 | ; interp. (make-vcat x h) has an 'x; postition and an 'h' happyness level 15 | 16 | (define CAT1 (make-vcat 0 90)) ; happy cat at origin 17 | (define CAT2 (make-vcat 50 50)) ; meh cat in middle of screen 18 | 19 | ; Cat -> ??? 20 | (define (fn-for-cat c) 21 | (... (... (cat-x-pos c)) 22 | (... (cat-happy c)))) 23 | 24 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/images/cat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/emaphis/HtDP2e-solutions/88b59b66a2f090dfd07290f16f8686c7e87298bf/1-Fixed-Size-Data/05-Adding-Structure/images/cat.png -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/images/chameleon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/emaphis/HtDP2e-solutions/88b59b66a2f090dfd07290f16f8686c7e87298bf/1-Fixed-Size-Data/05-Adding-Structure/images/chameleon.png -------------------------------------------------------------------------------- /1-Fixed-Size-Data/05-Adding-Structure/readme.txt: -------------------------------------------------------------------------------- 1 | ;; HtDP 2e - 5 Adding Structure 2 | 3 | Sometimes you need to store more than one piece of information with an entity 4 | structures create an entity with more than one piece of data 5 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/06-Itemizations-Structures/02_Mixing_Up_Worlds.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 02_Mixing_Up_Worlds) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 6 Itemizations and Structures 5 | ;; 6.2 Mixing Up Worlds 6 | ;; Exercises: 106-109 7 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/06-Itemizations-Structures/04_Checking_the_World.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 06_04_checking_the_world) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 6 Itemizations and Structures 5 | ;; 6.4 Checking the World 6 | ;; Exercise: 114 7 | 8 | ;;;;;;;;;;;;;;;;;;;;;;;;; 9 | ;; 6.4 Checking the World 10 | 11 | 12 | ;; world programs can deal with a lot of variing data so "bigban' comes with a 13 | ;; data verification mechanism 14 | 15 | #; 16 | (define (main s0) 17 | (big-bang so [...] [check-with number?] [...])) 18 | 19 | ;; examples: 20 | 21 | ; A UnitWorld is a number 22 | ; between 0 (inclusize and 1 (exclosive) 23 | ; [0,1) 24 | 25 | 26 | ; Any -> Boolean 27 | ; is x beween 0 (iclusive and 1 (exclusive) 28 | 29 | (check-expect (between-0-and-1? "a") #false) 30 | (check-expect (between-0-and-1? 1.2) #false) 31 | (check-expect (between-0-and-1? 0.2) #true) 32 | (check-expect (between-0-and-1? 0.0) #true) 33 | (check-expect (between-0-and-1? 1.0) #false) 34 | 35 | (define (between-0-and-1? x) 36 | (and (number? x) (<= 0 x) (< x 1))) 37 | 38 | #; 39 | (define (main s0) 40 | (big-bang s0 41 | ... 42 | [check-with between-0-and-1?] 43 | ...)) 44 | 45 | 46 | ;;;;;;;;;;;;;;; 47 | ;; Ex. 114: 48 | ;; Use the predicates from exercise 113 to check the space invader world 49 | ;; program, the virtual pet program (exercise 106), and the editor program 50 | ;; (A Graphical Editor). 51 | 52 | ;; See 06_04_space_invater_3.rkt, 06_04_chat_cham_4.rkt, 06_04_editor_4.rkt 53 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/06-Itemizations-Structures/05_Equality_Predicates.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 06_05_equality_predicates) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 6 Itemizations and Structures 5 | ;; 6.5 Equality Predicates 6 | ;; Exercise: 115 7 | 8 | 9 | ;; An equality predicate is a function that compares two elements of the same 10 | ;; collection of data. 11 | 12 | #| 13 | ; TrafficLight TrafficLight -> Boolean 14 | ; are the two (states of) traffic lights equal 15 | 16 | (check-expect (light=? "red" "red") #true) 17 | (check-expect (light=? "red" "green") #false) 18 | (check-expect (light=? "green" "green") #true) 19 | (check-expect (light=? "yellow" "yellow") #true) 20 | 21 | ; origina incorrect versionn 22 | 23 | (define (light=? a-value another-value) 24 | (string=? a-value another-value)) 25 | |# 26 | 27 | ; Any -> Boolean 28 | ; is the given value an element of TrafficLight 29 | (define (light? x) 30 | (cond 31 | [(string? x) (or (string=? "red" x) 32 | (string=? "green" x) 33 | (string=? "yellow" x))] 34 | [else #false])) 35 | 36 | (define MESSAGE 37 | "traffic light expected, given: some other value") 38 | 39 | ; Any Any -> Boolean 40 | ; are the two values elements of TrafficLight and, 41 | ; if so, are they equal 42 | 43 | (check-expect (light=? "red" "red") #true) 44 | (check-expect (light=? "red" "green") #false) 45 | (check-expect (light=? "green" "green") #true) 46 | (check-expect (light=? "yellow" "yellow") #true) 47 | 48 | (define (light=? a-value another-value) 49 | (if (and (light? a-value) (light? another-value)) 50 | (string=? a-value another-value) 51 | (error MESSAGE))) 52 | 53 | 54 | ;;;;;;;;;;;;;;;;;;; 55 | ;; Ex. 115: 56 | ;; Revise light=? so that the error message specifies which of the two 57 | ;; arguments aren’t elements of TrafficLight. 58 | 59 | (define MESSAGE-1 60 | "traffic light expected for parameter 1, given: some other value") 61 | 62 | (define MESSAGE-2 63 | "traffic light expected for parameter 2, given: some other value") 64 | 65 | ; Any Any -> Boolean 66 | ; are the two values elements of TrafficLight and, 67 | ; if so, are they equal 68 | 69 | (check-expect (light=?-2 "red" "red") #true) 70 | (check-expect (light=?-2 "red" "green") #false) 71 | (check-expect (light=?-2 "green" "green") #true) 72 | (check-expect (light=?-2 "yellow" "yellow") #true) 73 | 74 | (define (light=?-2 a-value another-value) 75 | (if (light? a-value) 76 | (if (light? another-value) 77 | (string=? a-value another-value) 78 | (error MESSAGE-2)) 79 | (error MESSAGE-1))) 80 | 81 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/06-Itemizations-Structures/ex094.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex094) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 94: 5 | ;; Draw some sketches of what the game scenery looks like at various stages. 6 | ;; Use the sketches to determine the constant and the variable pieces of the 7 | ;; game. For the former, develop physical and graphical constants that describe 8 | ;; the dimensions of the world (canvas) and its objects. Also develop some 9 | ;; background scenery. Finally, create your initial scene from the constants 10 | ;; for the tank, the UFO, and the background. 11 | 12 | ; Sketch done on paper. 13 | 14 | (require 2htdp/image) 15 | 16 | ;; Physical Constants 17 | 18 | (define WIDTH 200) 19 | (define HEIGHT 200) 20 | (define SCENE (empty-scene WIDTH HEIGHT "skyblue")) 21 | 22 | (define UFO-HEIGHT 20) 23 | (define UFO-WIDTH (* 2 UFO-HEIGHT)) 24 | (define UFO (overlay (circle (/ UFO-HEIGHT 2) "solid" "purple") 25 | (ellipse UFO-WIDTH (/ UFO-HEIGHT 2) "solid" "blue"))) 26 | 27 | (define TANK-HEIGHT 10) 28 | (define TANK-WIDTH (* 2 TANK-HEIGHT)) 29 | (define TANK (rectangle TANK-WIDTH TANK-HEIGHT "solid" "olive")) 30 | 31 | ;; starting positions 32 | (define UFO-X 100) 33 | (define UFO-Y 50) 34 | (define TANK-X 150) 35 | (define TANK-Y (- HEIGHT (/ TANK-HEIGHT 2))) 36 | 37 | ;; a mock-up 38 | (define MOCK-UP 39 | (place-image TANK TANK-X TANK-Y 40 | (place-image UFO UFO-X UFO-Y 41 | SCENE))) 42 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/06-Itemizations-Structures/ex095.txt: -------------------------------------------------------------------------------- 1 | 2 | ;;;;;;;;;;;;;;;;;;;;; 3 | ;; Ex. 95: 4 | ;; Explain why the three instances are generated according to the 5 | ;; first or second clause of the data definition. 6 | 7 | ; The first data definition is the only one that follows the first because it 8 | ; has only one representation, the second clause has two defintions since it 9 | ; modles two major states, just fired and in flight. 10 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/06-Itemizations-Structures/ex096.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname ex096) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; Ex. 96: 5 | ;; Sketch how each of the three game states could be rendered assuming 6 | ;; a 200 by 200 canvas. See MOCK-UP above 7 | 8 | 9 | (require 2htdp/image) 10 | 11 | ;; Physical Constants 12 | 13 | (define WIDTH 200) 14 | (define HEIGHT 200) 15 | (define SCENE (empty-scene WIDTH HEIGHT "skyblue")) 16 | 17 | (define UFO-HEIGHT 20) 18 | (define UFO-WIDTH (* 2 UFO-HEIGHT)) 19 | (define UFO (overlay (circle (/ UFO-HEIGHT 2) "solid" "purple") 20 | (ellipse UFO-WIDTH (/ UFO-HEIGHT 2) "solid" "blue"))) 21 | 22 | (define TANK-HEIGHT 10) 23 | (define TANK-WIDTH (* 2 TANK-HEIGHT)) 24 | (define TANK (rectangle TANK-WIDTH TANK-HEIGHT "solid" "olive")) 25 | 26 | (define MISSILE-WIDTH 1) 27 | (define MISSILE-HEIGHT 15) 28 | (define MISSILE 29 | (rectangle MISSILE-WIDTH MISSILE-HEIGHT "solid" "red")) 30 | 31 | 32 | ;; starting positions 33 | (define UFO-X 100) 34 | (define UFO-Y 50) 35 | (define MISSILE-Y 125) 36 | (define MISSILE-X 150) 37 | (define TANK-X 150) 38 | (define TANK-Y (- HEIGHT (/ TANK-HEIGHT 2))) 39 | 40 | ;; original mock-up 41 | (define MOCK-UP 42 | (place-image TANK TANK-X TANK-Y 43 | (place-image UFO UFO-X UFO-Y 44 | (place-image MISSILE MISSILE-X MISSILE-Y 45 | SCENE)))) 46 | 47 | 48 | ; an instance that describes the tank maneuvering into position to fire: 49 | ; the missile: 50 | ; (make-aim (make-posn 20 10) (make-tank 28 -3)) 51 | 52 | (define AIM-SCENE1 53 | (place-image TANK 58 TANK-Y 54 | (place-image UFO 50 30 55 | SCENE))) 56 | 57 | ; just like the previous one but the missile has been fired: 58 | ; (make-fired (make-posn 20 10) (make-tank 28 -3) 59 | ; (make-posn 28 (- HEIGHT TANK-HEIGHT))) 60 | 61 | (define FIRE-SCENE1 62 | (place-image TANK 60 TANK-Y 63 | (place-image UFO 50 60 64 | (place-image MISSILE 58 (- HEIGHT TANK-HEIGHT 10) 65 | SCENE)))) 66 | 67 | ; one where the missile is about to collide with the UFO: 68 | ; (make-fired (make-posn 20 100) (make-tank 100 3) (make-posn 22 103)) 69 | ; TANK -> UFO -> MISSLE 70 | (define FIRE-SCENE2 71 | (place-image TANK 60 TANK-Y 72 | (place-image UFO 50 100 73 | (place-image MISSILE 58 120 74 | SCENE)))) 75 | ;; Tests 76 | AIM-SCENE1 77 | FIRE-SCENE1 78 | FIRE-SCENE2 79 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/06-Itemizations-Structures/images/cat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/emaphis/HtDP2e-solutions/88b59b66a2f090dfd07290f16f8686c7e87298bf/1-Fixed-Size-Data/06-Itemizations-Structures/images/cat.png -------------------------------------------------------------------------------- /1-Fixed-Size-Data/06-Itemizations-Structures/images/chameleon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/emaphis/HtDP2e-solutions/88b59b66a2f090dfd07290f16f8686c7e87298bf/1-Fixed-Size-Data/06-Itemizations-Structures/images/chameleon.png -------------------------------------------------------------------------------- /1-Fixed-Size-Data/06-Itemizations-Structures/images/pedestrian_traffic_light_green.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/emaphis/HtDP2e-solutions/88b59b66a2f090dfd07290f16f8686c7e87298bf/1-Fixed-Size-Data/06-Itemizations-Structures/images/pedestrian_traffic_light_green.png -------------------------------------------------------------------------------- /1-Fixed-Size-Data/06-Itemizations-Structures/images/pedestrian_traffic_light_red.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/emaphis/HtDP2e-solutions/88b59b66a2f090dfd07290f16f8686c7e87298bf/1-Fixed-Size-Data/06-Itemizations-Structures/images/pedestrian_traffic_light_red.png -------------------------------------------------------------------------------- /1-Fixed-Size-Data/06-Itemizations-Structures/readme.txt: -------------------------------------------------------------------------------- 1 | 6.1 Designing with Itemizations, Again 2 | 3 | Designing a Space Invader game using Itemizations. 4 | -------------------------------------------------------------------------------- /1-Fixed-Size-Data/cat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/emaphis/HtDP2e-solutions/88b59b66a2f090dfd07290f16f8686c7e87298bf/1-Fixed-Size-Data/cat.png -------------------------------------------------------------------------------- /2-Arbitrarily-Large-Data/08-Lists/08_04_computing_lists.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 08_04_computing_lists) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 8 Lists 5 | ;; 8.4 Computing with Lists 6 | ;; Exercises: 135-136 7 | 8 | 9 | (define (contains-flatt? alon) 10 | (cond 11 | [(empty? alon) #false] 12 | [(cons? alon) 13 | (or (string=? (first alon) "Flatt") 14 | (contains-flatt? (rest alon)))])) 15 | 16 | 17 | ;; Eval: 18 | 19 | (contains-flatt? (cons "Flatt" (cons "C" '()))) 20 | ;== 21 | (cond 22 | [(empty? (cons "Flatt" (cons "C" '()))) #false] 23 | [(cons? (cons "Flatt" (cons "C" '()))) 24 | (or (string=? (first (cons "Flatt" (cons "C" '()))) "Flatt") 25 | (contains-flatt? (rest (cons "Flatt" (cons "C" '())))))]) 26 | ;== 27 | (cond 28 | [#false #false] 29 | [(cons? (cons "Flatt" (cons "C" '()))) 30 | (or (string=? (first (cons "Flatt" (cons "C" '()))) "Flatt") 31 | (contains-flatt? (rest (cons "Flatt" (cons "C" '())))))]) 32 | ;== 33 | (cond 34 | [(cons? (cons "Flatt" (cons "C" '()))) 35 | (or (string=? (first (cons "Flatt" (cons "C" '()))) "Flatt") 36 | (contains-flatt? (rest (cons "Flatt" (cons "C" '())))))]) 37 | ;== 38 | (cond 39 | [#true 40 | (or (string=? (first (cons "Flatt" (cons "C" '()))) "Flatt") 41 | (contains-flatt? (rest (cons "Flatt" (cons "C" '())))))]) 42 | ;== 43 | (or (string=? (first (cons "Flatt" (cons "C" '()))) "Flatt") 44 | (contains-flatt? (rest (cons "Flatt" (cons "C" '()))))) 45 | ;== 46 | (or (string=? "Flatt" "Flatt") 47 | (contains-flatt? (rest (cons "Flatt" (cons "C" '()))))) 48 | ;== 49 | (or #true 50 | (contains-flatt? (rest (cons "Flatt" (cons "C" '()))))) 51 | ;== 52 | #true 53 | 54 | 55 | ;;;;;;;;;;;;;;;;;;;; 56 | ;; Ex. 135: 57 | ;; Use DrRacket’s stepper to check the calculation for 58 | 59 | (contains-flatt? (cons "Flatt" (cons "C" '()))) 60 | ;== 61 | #true 62 | 63 | ;; Also use the stepper to determine the value of 64 | 65 | (contains-flatt? 66 | (cons "A" (cons "Flatt" (cons "C" '())))) 67 | ;== 68 | #true 69 | 70 | ;; What happens when "Flatt" is replaced with "B"? 71 | 72 | (contains-flatt? 73 | (cons "A" (cons "B" (cons "C" '())))) 74 | ;== 75 | #false 76 | 77 | 78 | ;; for exercist 136: 79 | 80 | (define-struct pair [left right]) 81 | 82 | 83 | ; A ConsOrEmpty is one of: 84 | ; – '() 85 | ; – (cons Any ConsOrEmpty) 86 | ; interpretation ConsOrEmpty is the class of all lists 87 | 88 | ; Any ConsOrEmpty -> ConsOrEmpty 89 | (define (our-cons a-value a-list) 90 | (cond 91 | [(empty? a-list) (make-pair a-value a-list)] 92 | [(pair? a-list) (make-pair a-value a-list)] 93 | [else (error "cons: second argument ...")])) 94 | 95 | ; ConsOrEmpty -> Any 96 | ; extracts the left part of the given pair 97 | (define (our-first a-list) 98 | (if (empty? a-list) 99 | (error 'our-first "...") 100 | (pair-left a-list))) 101 | 102 | ; ConsOrEmpty -> ConsOrEmpty 103 | ; extracts the right part of athe given pair 104 | (define (our-rest a-list) 105 | (if (empty? a-list) 106 | (error 'our-first "...") 107 | (pair-right a-list))) 108 | 109 | ;; Ex. 136: 110 | ;; Validate with DrRacket’s stepper 111 | 112 | ; (our-first (our-cons "a" '())) == "a" 113 | ; (our-rest (our-cons "a" '())) == "a" 114 | -------------------------------------------------------------------------------- /2-Arbitrarily-Large-Data/09-Designing-Self-Referential/09_04_russian_dolls.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 09_04_russian_dolls) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 9 Designing with Self-Referential Data Definitions 5 | ;; 9.4 Russian Dolls 6 | ;; Exercises: 154, 155 7 | 8 | (require 2htdp/image) 9 | 10 | (define-struct layer [color doll]) 11 | 12 | ; An RD (short for Russian doll) is one of: 13 | ; – String 14 | ; – (make-layer String RD) 15 | 16 | (define RD1 "red") 17 | (define RD2 (make-layer "green" "red")) 18 | (define RD3 (make-layer "yellow" (make-layer "green" "red"))) 19 | 20 | #; ; template 21 | (define (fun-for-rd rd) 22 | (cond [(string? rd) (... rd)] 23 | [(layer? rd) 24 | (... (layer-color rd) 25 | (fun-for-rd (layer-doll rd)))])) 26 | 27 | 28 | ; RD -> Number 29 | ; how many dolls are part on an-rd 30 | 31 | (check-expect (depth "red") 1) 32 | 33 | (check-expect (depth (make-layer "yellow" 34 | (make-layer "green" "red"))) 35 | 3) 36 | 37 | (define (depth an-rd) 38 | (cond [(string? an-rd) 1] 39 | [(layer? an-rd) 40 | (+ (depth (layer-doll an-rd)) 1)])) 41 | 42 | 43 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 44 | ;; Ex. 154: 45 | ;; Design the function colors. It consumes a Russian doll and produces a string 46 | ;; of all colors, separate by a comma and a space. 47 | ;; Thus our example should produce: "yellow, green, red" 48 | 49 | ; RD -> Color 50 | ; produce a String of all the colors, separated by comma and a space 51 | 52 | (check-expect (colors "red") "red") 53 | 54 | (check-expect (colors (make-layer "yellow" 55 | (make-layer "green" "red"))) 56 | "yellow, green, red") 57 | 58 | 59 | (define (colors an-rd) 60 | (cond [(string? an-rd) an-rd] 61 | [(layer? an-rd) 62 | (string-append (layer-color an-rd) ", " 63 | (colors (layer-doll an-rd)))])) 64 | 65 | 66 | ;; Ex. 155: 67 | ;; Design the function inner, which consumes an RD and produces the 68 | ;; (color of the) innermost doll. 69 | ;; Use DrRacket’s stepper to evaluate (inner rd) for you favorite rd. 70 | 71 | ; RD -> Color 72 | ; produce the color of the innermost doll 73 | 74 | (check-expect (inner "red") "red") 75 | 76 | (check-expect (inner (make-layer "yellow" 77 | (make-layer "green" "red"))) 78 | "red") 79 | 80 | (define (inner an-rd) 81 | (cond [(string? an-rd) an-rd] 82 | [(layer? an-rd) 83 | (inner (layer-doll an-rd))])) 84 | 85 | ; evaluates in 16 steps 86 | ;(inner (make-layer "yellow" (make-layer "green" "red"))) 87 | -------------------------------------------------------------------------------- /2-Arbitrarily-Large-Data/09-Designing-Self-Referential/09_05_lists_and_world.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 09_05_lists_and_world) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 9 Designing with Self-Referential Data Definitions 5 | ;; 9.5 Lists and World 6 | ;; Exercises: 156-159 7 | 8 | ;; Sample Problem 9 | ;; Design a world program that simulates firing shots. Every time the “player” 10 | ;; hits the space bar, the program adds a shot to the bottom of the canvas. 11 | ;; These shots rise vertically at the rate of one pixel per tick. 12 | 13 | (require 2htdp/image) 14 | (require 2htdp/universe) 15 | 16 | 17 | (define HEIGHT 80) ; distances in terms of pixels 18 | (define WIDTH 100) 19 | (define XSHOTS (/ WIDTH 2)) 20 | 21 | ; graphical constants 22 | (define BACKGROUND (empty-scene WIDTH HEIGHT)) 23 | (define SHOT (triangle 3 "solid" "red")) 24 | 25 | 26 | ; A List-of-shots is one of: 27 | ; – '() 28 | ; – (cons Shot List-of-shots) 29 | ; interpretation the collection of shots fired 30 | 31 | ; A Shot is a Number. 32 | ; interpretation represents the shot's y-coordinate 33 | 34 | ; A ShotWorld is List-of-numbers. 35 | ; interpretation each number on such a list 36 | ; represents the y-coordinate of a shot 37 | 38 | 39 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 40 | ;; function defininitons 41 | 42 | ; ShotWorld -> ShotWorld 43 | (define (main w0) 44 | (big-bang w0 45 | [on-tick tock] 46 | [on-key keyh] 47 | [to-draw to-image])) 48 | 49 | ; ShotWorld -> Image 50 | ; adds the image of a shot for each y on w 51 | ; at (MID,y} to the background image 52 | (check-expect (to-image '()) BACKGROUND) 53 | (check-expect (to-image (cons 9 '())) 54 | (place-image SHOT XSHOTS 9 BACKGROUND)) 55 | (check-expect (to-image (cons 50 (cons 9 '()))) 56 | (place-image SHOT XSHOTS 50 57 | (place-image SHOT XSHOTS 9 BACKGROUND))) 58 | 59 | ;(define (to-image w) BACKGROUND) 60 | ; ShotWorld -> Image 61 | (define (to-image w) 62 | (cond 63 | [(empty? w) BACKGROUND] 64 | [else 65 | (place-image SHOT XSHOTS (first w) 66 | (to-image (rest w)))])) 67 | 68 | ; ShotWorld -> ShotWorld 69 | ; moves each shot on w up by one pixel 70 | (define (tock w) 71 | (cond 72 | [(empty? w) '()] 73 | [else 74 | (cons (sub1 (first w)) 75 | (tock (rest w)))])) 76 | 77 | ; ShotWorld KeyEvent -> ShotWorld 78 | ; adds a shot to the world 79 | ; if the player pressed the space bar 80 | (define (keyh w ke) 81 | (if (key=? ke " ") (cons HEIGHT w) w)) 82 | 83 | 84 | ;; Ex. 156: 85 | ;; Equip the program in figure 57 with tests and make sure it passes those. 86 | ;; Explain what main does. Then run the program via main 87 | ;; See: 09_05_156_shot_world1.rkt 88 | 89 | ;; Ex. 157: 90 | ;; See 09_05_157_shot_world2.rkt 91 | 92 | ;; eX. 158: 93 | ;; See: 09_05_158_shot_world3.rkt 94 | 95 | ;; Ex. 159: 96 | ;; See 09_05_159_lecture_hall2.rkt 97 | 98 | -------------------------------------------------------------------------------- /2-Arbitrarily-Large-Data/10-More-on-Lists/10_01_161_wages1.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 10_01_161_wages1) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 10 More on Lists 5 | ;; 10.1 Functions that Produce Functions 6 | ;; Exercise: 161 7 | ;; Wage 2 8 | 9 | ;; Ex. 161 10 | ;; Translate the examples into tests and make sure they all succeed. Then 11 | ;; change the function in figure 60 so that everyone gets $14 per hour. 12 | ;; Now revise the entire program so that changing the wage for everyone is a 13 | ;; single change to the entire program and not several. 14 | 15 | ;(define PAY-RATE 12) 16 | (define PAY-RATE 14) 17 | 18 | ; Number -> Number 19 | ; computes the wage for h hours of work 20 | (check-expect (wage 10) (* PAY-RATE 10)) 21 | 22 | (define (wage h) 23 | (* PAY-RATE h)) 24 | 25 | 26 | ; calculate wages for a list of employees 27 | 28 | ; List-of-numbers -> List-of-numbers 29 | ; computes the weekly wages for the weekly hours 30 | 31 | (check-expect (wage* '()) '()) 32 | (check-expect (wage* (cons 28 '())) (cons (* PAY-RATE 28) '())) 33 | (check-expect (wage* (cons 4 (cons 2 '()))) 34 | (cons (* PAY-RATE 4) (cons (* PAY-RATE 2) '()))) 35 | 36 | (define (wage* whrs) 37 | (cond 38 | [(empty? whrs) '()] 39 | [else (cons (wage (first whrs)) 40 | (wage* (rest whrs)))])) 41 | -------------------------------------------------------------------------------- /2-Arbitrarily-Large-Data/10-More-on-Lists/10_01_162_wages2.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 10_01_162_wages2) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 10 More on Lists 5 | ;; 10.1 Functions that Produce Functions 6 | ;; Exercise: 162 7 | ;; Wages 2 8 | 9 | ;; Ex. 162 10 | ;; No employee could possibly work more than 100 hours per week. To protect the 11 | ;; company against fraud, the function should check that no item of the input 12 | ;; list of wage* exceeds 100. If one of them does, the function should 13 | ;; immediately signal an error. How do we have to change the function in 14 | ;; figure 60 if we want to perform this basic reality check? 15 | 16 | 17 | (define PAY-RATE 12) 18 | (define OVERTIME-LIMIT 100) 19 | 20 | 21 | ; Number -> Number 22 | ; computes the wage for h hours of work 23 | (check-expect (wage 10) (* PAY-RATE 10)) 24 | 25 | (define (wage h) 26 | (* PAY-RATE h)) 27 | 28 | 29 | ; calculate wages for a list of employees 30 | 31 | ; List-of-numbers -> List-of-numbers or Error 32 | ; computes the weekly wages for the weekly hours 33 | ; produce "error" if time limit is exceeded 34 | 35 | (check-expect (wage* '()) '()) 36 | (check-expect (wage* (cons 28 '())) (cons (* PAY-RATE 28) '())) 37 | (check-expect (wage* (cons 4 (cons 2 '()))) 38 | (cons (* PAY-RATE 4) (cons (* PAY-RATE 2) '()))) 39 | (check-error (wage* (cons 10 (cons 100 '())))) 40 | 41 | (define (wage* whrs) 42 | (cond 43 | [(empty? whrs) '()] 44 | [else (if (reasonable? (first whrs)) 45 | (cons (wage (first whrs)) 46 | (wage* (rest whrs))) 47 | (error "Error: found too much overtime"))])) 48 | 49 | 50 | ;; Number -> Boolean 51 | ;; produce #true time is not below OVERTIME-LIMIT 52 | (check-expect (reasonable? 99) #true) 53 | (check-expect (reasonable? 100) #false) 54 | 55 | (define (reasonable? n) 56 | (< n OVERTIME-LIMIT)) -------------------------------------------------------------------------------- /2-Arbitrarily-Large-Data/10-More-on-Lists/10_01_163_FTC.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 10_01_163_FTC) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 10 More on Lists 5 | ;; 10.1 Functions that Produce Functions 6 | ;; Exercise: 163 7 | ;; Fahrenheit to Celcius 8 | 9 | 10 | ;; Ex. 163: 11 | ;; Design convertFC. The function converts a list of measurements in Fahrenheit 12 | ;; to a list of Celsius measurements. 13 | 14 | 15 | 16 | ; List-of-Temps -> List-of-Temps 17 | ; convert a list of Temps in Fahrenhieght to a List of Temps is Celcius 18 | 19 | (check-expect (f->c* '()) '()) 20 | (check-expect (f->c* (cons -40 (cons 32 (cons 68 (cons 212 '()))))) 21 | (cons -40 (cons 0 (cons 20 (cons 100 '()))))) 22 | 23 | (define (f->c* lot) 24 | (cond 25 | [(empty? lot) '()] 26 | [else (cons (f->c (first lot)) 27 | (f->c* (rest lot)))])) 28 | 29 | 30 | ; Number -> Number 31 | ; convert a temp in Fahrenheit to a temp in Celcius 32 | (check-expect (f->c -40) -40) 33 | (check-expect (f->c 32) 0) 34 | (check-expect (f->c 68) 20) 35 | (check-expect (f->c 212) 100) 36 | (define (f->c f) 37 | (* 5/9 (- f 32))) 38 | -------------------------------------------------------------------------------- /2-Arbitrarily-Large-Data/10-More-on-Lists/10_01_164_dollars_to_euros.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 10_01_164_dollars_to_euros) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 10 More on Lists 5 | ;; 10.1 Functions that Produce Functions 6 | ;; Exercise: 164 7 | ;; Dollar to Euro Converter 8 | 9 | 10 | ;; Ex. 164: 11 | ;; Design the function convert-euro, which converts a list of US$ amounts into 12 | ;; a list of € amounts. Look up the current exchange rate on the web. 13 | 14 | ;; Generalize convert-euro to the function convert-euro*, which consumes an 15 | ;; exchange rate and a list of US$ amounts and converts the latter into a list 16 | ;; of € amounts. 17 | 18 | (define DE-RATE 0.89) ; as of 24/9/2016 19 | 20 | 21 | ;; Number -> Number 22 | ;; Convert a Dollar to a Euro ammount with a given ratio 23 | (check-expect (convert 0.0) 0.0) 24 | (check-expect (convert 10.0) (* DE-RATE 10.0)) 25 | 26 | (define (convert d) 27 | (* DE-RATE d)) 28 | 29 | 30 | 31 | ;; List-of-number -> List-of-number 32 | ;; produce a list of Euros converted from a list of Dollars 33 | 34 | (check-expect (convert-euro '()) '()) 35 | (check-expect (convert-euro (cons 0.0 (cons 10.0 (cons 15.15 '())))) 36 | (cons 0.0 (cons (* DE-RATE 10.0) (cons (* DE-RATE 15.15) '())))) 37 | 38 | (define (convert-euro lod) 39 | (cond 40 | [(empty? lod) '()] 41 | [else (cons (convert (first lod)) 42 | (convert-euro (rest lod)))])) 43 | 44 | 45 | ;; generalized version: 46 | 47 | ;; List-of-number Number -> List-of-number 48 | ;; produce a list of Euros converted from a list of Dollars and 49 | ;; and exchange rate 50 | 51 | (check-expect (convert-euro* '() DE-RATE) '()) 52 | (check-expect (convert-euro* (cons 0.0 (cons 10.0 (cons 15.15 '()))) DE-RATE) 53 | (cons 0.0 (cons (* DE-RATE 10.0) (cons (* DE-RATE 15.15) '())))) 54 | 55 | (define (convert-euro* lod rt) 56 | (cond 57 | [(empty? lod) '()] 58 | [else (cons (* (first lod) rt) 59 | (convert-euro (rest lod)))])) 60 | -------------------------------------------------------------------------------- /2-Arbitrarily-Large-Data/10-More-on-Lists/10_01_165_subst_robot.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 10_01_165_subst_robot) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 10 More on Lists 5 | ;; 10.1 Functions that Produce Functions 6 | ;; Exercise: 165 7 | ;; Substitute Robot 8 | 9 | 10 | ;; Ex. 165: 11 | ;; Design the function subst-robot, which consumes a list of toy descriptions 12 | ;; (one-word strings) and replaces all occurrences of "robot" with "r2d2"; all 13 | ;; other descriptions remain the same. 14 | 15 | ;; Generalize subst-robot to substitute. The latter consumes two strings, 16 | ;; called new and old, and a list of strings. It produces a new list of strings 17 | ;; by substituting all occurrences of old with new. 18 | 19 | 20 | 21 | ;; List-of-strings -> List-of-strings 22 | ;; consumes a List of Strings and replaces "robot" with "r2d2" 23 | 24 | (check-expect (subst-robot '()) '()) 25 | (check-expect (subst-robot (cons "car" (cons "robot" (cons "doll" '())))) 26 | (cons "car" (cons "r2d2" (cons "doll" '())))) 27 | 28 | (define (subst-robot lot) 29 | (cond 30 | [(empty? lot) '()] 31 | [else (if (string=? (first lot) "robot") 32 | (cons "r2d2" (subst-robot (rest lot))) 33 | (cons (first lot) (subst-robot (rest lot))))])) 34 | 35 | 36 | ;; generalized version: 37 | 38 | ;; List-of-strings String String -> List-of-strings 39 | ;; consumes a List of Strings and replaces 'old' String with 'new' String 40 | 41 | (check-expect (substitute '() "robot" "r2d2") '()) 42 | (check-expect (substitute (cons "car" 43 | (cons "robot" 44 | (cons "doll" '()))) 45 | "robot" "r2d2") 46 | (cons "car" (cons "r2d2" (cons "doll" '())))) 47 | 48 | (define (substitute lot old new) 49 | (cond 50 | [(empty? lot) '()] 51 | [else (if (string=? (first lot) old) 52 | (cons new (substitute (rest lot) old new)) 53 | (cons (first lot) 54 | (substitute (rest lot) old new)))])) 55 | 56 | -------------------------------------------------------------------------------- /2-Arbitrarily-Large-Data/10-More-on-Lists/10_01_producing_lists.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 10_01_producing_lists) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 10 More on Lists 5 | ;; 10.1 Functions that Produce Functions 6 | ;; Exercises: 161-165 7 | 8 | 9 | ;; Here is a function for determining the wage of an hourly employee: 10 | 11 | ; Number -> Number 12 | ; computes the wage for h hours of work 13 | (define (wage h) 14 | (* 12 h)) 15 | 16 | 17 | ; calculate wages for a list of employees 18 | 19 | ; List-of-numbers -> List-of-numbers 20 | ; computes the weekly wages for the weekly hours 21 | 22 | (check-expect (wage* '()) '()) 23 | (check-expect (wage* (cons 28 '())) (cons 336 '())) 24 | (check-expect (wage* (cons 4 (cons 2 '()))) (cons 48 (cons 24 '()))) 25 | 26 | (define (wage* whrs) 27 | (cond 28 | [(empty? whrs) '()] 29 | [else (cons (wage (first whrs)) 30 | (wage* (rest whrs)))])) 31 | 32 | 33 | ;;;;;;;;;;;;;;;;;;;;;;;; 34 | ;; Ex. 161 35 | ;; See 10_01_161_wages1.rkt 36 | 37 | ;; Ex. 162 38 | ;; See 10_01_162_wages2.rkt 39 | 40 | ;; Ex. 163 41 | ;; See 10_01_163_FTC.rkt 42 | 43 | ;; Ex. 164 44 | ;; See 10_01_164_dollars_to_euros.rkt 45 | 46 | ;; Ex. 165: 47 | ;; See 10_01_165_subst_robot.rkt 48 | 49 | 50 | -------------------------------------------------------------------------------- /2-Arbitrarily-Large-Data/10-More-on-Lists/10_02_166a_Employees3.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-reader.ss" "lang")((modname 10_02_166a_Employees3) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | ;; HtDP 2e - 10 More on Lists 5 | ;; 10.2 Structures in Lists 6 | ;; Exercise: 166-a 7 | ;; Employees-3 8 | 9 | ;; Ex. 166: 10 | ;; The wage*.v2 function consumes a list of work records and produces a list of 11 | ;; numbers. Of course, functions may also produce lists of structures. 12 | 13 | ;; Develop a data representation for pay checks. Assume that a pay check 14 | ;; contains two distinctive pieces of information: the employee’s name and an 15 | ;; amount. Then design the function wage*.v3. It consumes a list of work 16 | ;; records and computes a list of pay checks from it, one per record. 17 | 18 | ;; In reality, a pay check also contains an employee number. Develop a data 19 | ;; representation for employee information and change the data definition for 20 | ;; work records so that it uses employee information and not just a string for 21 | ;; the employee’s name. Also change your data representation of pay checks so 22 | ;; that it contains an employee’s name and number, too. Finally, design 23 | ;; wage*.v4, a function that maps lists of revised work records to lists of 24 | ;; revised pay checks. 25 | 26 | ;; version 3 27 | 28 | (define-struct work [employee rate hours]) 29 | ; A (piece of) Work is a structure: 30 | ; (make-work String Number Number) 31 | ; interpretation (make-work n r h) combines the name 32 | ; with the pay rate r and the number of hours h 33 | 34 | (define WORK1 (make-work "Robby" 11.95 39)) 35 | 36 | 37 | (define-struct pay-check [name amount]) 38 | ; Pay-check is a (make-paycheck String Number) 39 | ; where name is employee's name and amount of check 40 | ; in dollars 41 | 42 | (define PAY-CHECK1 (make-pay-check "Robby" "446.60")) 43 | 44 | (define (fun-for-pay-check pc) 45 | (... (pay-check-name pc) 46 | (pay-check-amount pc))) 47 | 48 | 49 | ; Lop (list of paychecks) is one of: 50 | ; - '() 51 | ; - (cons Paycheck Lop) 52 | ; interpretation: an instance of Lop represents pay checkes for a week 53 | 54 | 55 | ; Low -> List-of-pay-checks 56 | ; computes the weekly wages for the given records 57 | (check-expect (wage*.v3 '()) '()) 58 | (check-expect (wage*.v3 (cons (make-work "Robby" 11.95 39) 59 | '())) 60 | (cons (make-pay-check "Robby" (* 11.95 39)) '())) 61 | 62 | 63 | (define (wage*.v3 an-low) 64 | (cond 65 | [(empty? an-low) '()] 66 | [(cons? an-low) 67 | (cons (wage.v3 (first an-low)) 68 | (wage*.v3 (rest an-low)))])) 69 | 70 | ; Work -> Pay-check 71 | ; computes the wage for the given work record w 72 | (define (wage.v3 w) 73 | (make-pay-check (work-employee w) 74 | (* (work-rate w) (work-hours w)))) 75 | -------------------------------------------------------------------------------- /2-Arbitrarily-Large-Data/10-More-on-Lists/ttt.txt: -------------------------------------------------------------------------------- 1 | TTT 2 | 3 | Put up in a place 4 | where it's easy to see 5 | the cryptic admonishment 6 | T.T.T. 7 | 8 | When you feel how depressingly 9 | slowly you climb, 10 | it's well to remember that 11 | Things Take Time. 12 | 13 | Piet Hein 14 | -------------------------------------------------------------------------------- /2-Arbitrarily-Large-Data/12-Projects-Lists/test1.rkt: -------------------------------------------------------------------------------- 1 | ;; The first three lines of this file were inserted by DrRacket. They record metadata 2 | ;; about the language level of this file in a form that our tools can easily process. 3 | #reader(lib "htdp-beginner-abbr-reader.ss" "lang")((modname test1) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) 4 | #; 5 | (check-expect (slice "e" "hllo") (list (append "" "e" "hllo") 6 | (append "h" "e" "llo") 7 | (append "hl" "e" "lo") 8 | (append "hll" "e" "o") 9 | (append "hllo" "e" ""))) 10 | #; 11 | (check-expect (slice "e" (list "h" "l" "l" "o")) 12 | (list (list '() (list "h" "l" "l" "o")) 13 | (list (list "h") (list "l" "l" "o")) 14 | (list (list "h" "l") (list "l" "o")) 15 | (list (list "h" "l" "l") (list "o")) 16 | (list (list "h" "l" "l" "o") '()))) 17 | 18 | 19 | (define (slice ch wrd) 20 | (cond [(empty? wrd) '()] 21 | [else 22 | (cons (cons ch (first wrd)) 23 | (slice ch (rest wrd)))])) 24 | 25 | -------------------------------------------------------------------------------- /notes/basicsteps.txt: -------------------------------------------------------------------------------- 1 | Figure 5 from preface: 2 | 3 | 1 From Problem Analysis to Data Definitions 4 | 5 | Identify the information that must be represented and how it is represented in the chosen programming language. Formulate data definitions and illustrate them with examples. 6 | 7 | 2 Signature, Purpose Statement, Header 8 | 9 | State what kind of data the desired function consumes and produces. Formulate a concise answer to the question what the function computes. Define a stub that lives up to the signature. 10 | 11 | 3 Functional Examples 12 | 13 | Work through examples that illustrate the function’s purpose. 14 | 15 | 4 Function Template 16 | 17 | Translate the data definitions into an outline of the function. 18 | 19 | 5 Function Definition 20 | 21 | Fill in the gaps in the function template. Exploit the purpose statement and the examples. 22 | 23 | 6 Testing 24 | 25 | Articulate the examples as tests and ensure that the function passes all. Doing so discovers mistakes. Tests also supplement examples in that they help others read and understand the definition when the need arises—and it will arise for any serious program. 26 | 27 | Figure 5: The basic steps of a function design recipe 28 | -------------------------------------------------------------------------------- /notes/definitions.md: -------------------------------------------------------------------------------- 1 | A BSL program consists of several definitions followed by an expression that involves those definitions. 2 | 3 | Two kinds of defintions: 4 | 5 | - constant definitions, of the shape 6 | 7 | ''' 8 | (define Variable Expression) 9 | ''' 10 | 11 | - function definitions, 12 | 13 | ''' 14 | (define (functionName Variable ... Variable ) 15 | Expression) 16 | ''' 17 | --------------------------------------------------------------------------------