02 Design Post
02 Design Post
Readings:
Programs as communication
Every program is an act of communication:
between you and the computer between you and yourself in the future between you and others
Comments
Comment: information important to humans but ignored by the computer Code: anything that is not a comment; machine-readable ;; Constants for calendars (dene year-days 365) ; not a leap year Use one semicolon to indicate that the rest of the line is a comment. Use two semicolons to start a line that is a comment. Do not use DrRackets Comment Boxes.
CS 115 Winter 2013 2: The design recipe
reliability (the function has been tested) readability (comments make it understandable)
You will use the design recipe for every function throughout the course. The basic recipe will be modied as we add new ingredients.
CS 115 Winter 2013 2: The design recipe
Types in contracts
Our rules will be more strict than those used in the textbook.
Scheme data types: num (any numeric value). User-dened data types. int for any integer, such as in quotient. nat for any natural number (including 0) (used later). any for any Scheme value. We will add to this list; see Style Guide for details.
CS 115 Winter 2013 2: The design recipe
RRR
Step 1: Contract and function header ;; sum-of-squares: num num num (dene (sum-of-squares arg1 arg2) Step 2: Purpose ;; Purpose: produces sum of squares of arg1 and arg2.
10
Step 3: Examples ;; Examples: (sum-of-squares 3 4) 25 ;; (sum-of-squares 0 2.5) 6.25 Step 4: Body (added to function header) (dene (sum-of-squares arg1 arg2) (+ ( arg1 arg1) ( arg2 arg2)))
11
;; sum-of-squares: num num num ;; Purpose: produces sum of squares of arg1 and arg2. ;; Examples: (sum-of-squares 3 4) 25 ;; (sum-of-squares 0 2.5) 6.25 (dene (sum-of-squares arg1 arg2) (+ ( arg1 arg1) ( arg2 arg2))) In your work: Single arrow as minus and greater-than (>) Double arrow as equals and greater-than (=>)
12
Testing in DrRacket
Use (check-expect fun-application value-expected). Each test is targeted to focus on a specic aspect of the code. Tests typically include all the examples, plus some more. Tests dont need to be big. Smaller is often better, because it is easier to gure out what went wrong if they fail. The number needed is a matter of judgement. Figure out expected answers by hand, not by running the program.
13
;; sum-of-squares: num num num ;; Purpose: produces sum of squares of arg1 and arg2. ;; Examples: (sum-of-squares 3 4) 25 ;; (sum-of-squares 0 2.5) 6.25 (dene (sum-of-squares arg1 arg2) (+ ( arg1 arg1) ( arg2 arg2))) ;; Tests for sum-of-squares: (check-expect (sum-of-squares 3 4) 25) (check-expect (sum-of-squares 0 2.5) 6.25) (check-expect (sum-of-squares 1 1) 2)
14
Note: check-expect only works with exact numbers. The following code will result in an error. (dene (circle-area r) ( pi r r)) (check-expect (circle-area 1) pi) Use check-within with a function application, a value, and a range within which the answer can differ from the value provided. A range of .00001 should sufce. (check-within (circle-area 1) pi .00001)
15
String functions: (string-append "now" "here") "nowhere" (string-length "length") 6 (substring "caterpillar" 5 9) "pill" (substring "cat" 0 0) "" See the course Web site (not in the textbook).
CS 115 Winter 2013 2: The design recipe
16
17
;; swap-parts: string string ;; Purpose: produces a string formed by swapping the ;; front and back of str. ;; Examples: (swap-parts angle) glean ;; (swap-parts potshots) hotspots (dene (swap-parts str) Subtasks: nding the midpoint, extracting the front, extracting the back.
18
Helper functions
A helper function is a function that is used in the main function to
generalize similar expressions, express repeated computations, and avoid long, unreadable function denitions.
Choose meaningful function names for all functions and parameters, including helper functions (not helper). Use the full design recipe to create helper functions. Put helper functions before the main function. Indentation and style guidelines available on the course Web site.
CS 115 Winter 2013 2: The design recipe
19
20
21
22
Developing swap-parts
;; swap-parts: string string ;; Purpose: produces a string formed by swapping the back ;; and front of str. ;; Examples: (swap-parts angle) glean ;; (swap-parts potshots) hotspots (dene (swap-parts str) (string-append (back-part str) (front-part str))) ;; Tests for swap-parts (check-expect (swap-parts "angle") "glean") (check-expect (swap-parts "potshots") "hotspots")
CS 115 Winter 2013 2: The design recipe
23
100 free daytime minutes 200 free evening minutes 1 dollar per minute charge for each additional daytime minute 50 cent per minute charge for each additional evening minute
How can we remember the meaning of each number?
CS 115 Winter 2013 2: The design recipe
24
Using constants
;; Constants for phone plan ;; Free limits (dene day-free 100) (dene eve-free 200) ;; Rates per minute (dene day-rate 1) (dene eve-rate .5) Use constants for readability and exibility. Put them before helper functions and main functions.
CS 115 Winter 2013 2: The design recipe
25
;; cell-bill: nat nat num ;; Purpose: produces cell phone bill for day and eve minutes. ;; Examples: (cell-bill 101 0) 1 ;; (cell-bill 99 0) 0 ;; (cell-bill 0 199) 0 ;; (cell-bill 0 202) 1 ;; (cell-bill 150 300) 100 (dene (cell-bill day eve)
26
Developing charges-for
;; charges-for: nat nat num num ;; Purpose: produces charges for minutes, ;; given the rate per minute past the freelimit. ;; Examples: (charges-for 101 100 5) 5 ;; (charges-for 99 100 34) 0 (dene (charges-for minutes freelimit rate) (max 0 ( ( minutes freelimit) rate))) ;; Tests for charges-for: (check-expect (charges-for 101 100 5) 5) (check-expect (charges-for 99 100 34) 0)
CS 115 Winter 2013 2: The design recipe
27
Completing cell-bill
(dene (cell-bill day eve) (+ (charges-for day day-free day-rate) (charges-for eve eve-free eve-rate))) ;; Tests for cell-bill (check-expect (cell-bill 101 0) 1) (check-expect (cell-bill 99 0) 0) (check-expect (cell-bill 0 199) 0) (check-expect (cell-bill 0 202) 1) (check-expect (cell-bill 150 300) 100)
CS 115 Winter 2013 2: The design recipe
28
30
You should start to use the design recipe and appropriate coding style for all Scheme programs you write. You should look for opportunities to use helper functions and constants to structure your programs, and gradually learn when and where they are appropriate. You should be able to use strings in labs, assignments, and exams.
31