;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-beginner-reader.ss" "lang")((modname start-design-recipe-before) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) #| `define`: a keyword to bind an identifier to a value: |# (require "student-extras.rkt") ; includes `natural?` `non-negative-integer?`, etc. ; Reminder: Syntax for define-a-variable: ; (define ) ; Reminder: Syntax for call-a-function (preliminary): ; ( …) #| The Design Recipe (take 1 -- primitive types only) ------- per function: 4. tests (consider: 0,1,many; strings/lists/arrays/collections of length 0,1,many; fractions & negatives 5. SSS: signature, purpose-statement, and stub (purpose-statement should mention each arg by name; always mention units if there are any) 7. complete the body-expression 8. watch your tests pass |# ;;;;;;;;;;;; Example, of design recipe ;;;;;;;;;;;;;;;;;;;;; (check-expect (pizza-area 18) 254.5) ; Make a few more tests! ; `check-expect` and `check-within` are part of the language. ; Test-cases include an actual-call (with specific inputs), ; and a specific expected-result. They are *runnable code*! ; ; (comparable to junit's `AssertEquals(Object,Object)` ; and `AssertEquals(double,double,double)`). ; pizza-area : non-negative real -> non-negative real ; The area (in sq.in.) of a pizza, whose diameter is `diam` (in inches) ; (define (pizza-area diam) ;(-> real? real?) 17 #| TODO in class |#) ; New syntax: `define` for functions. ; Note how it mirrors how the function is called (but params instead of argument-expressions). ; Syntax for define-a-function: ; #| TODO in class |# ;;; EXAMPLE: Together, we'll write: `monogram`: ; Given a first name and last name, return a monogram with initials: #;(check-expect (monogram "Ian" "Barland") "i.b.") ;; We've repeated the 'substring', 'string-downcase', and appending-to-"." twice! ;; Should our boss ever change our requirements, we'll have to apply our changes to ;; *both* parts of the code (and if we forget, we'll have a bug). ;; Poor design, not having a single point-of-control. ;; ;; Now, let's re-factor `monogram`, to get rid of the repeated code! ;;; EXAMPLE: Okay, now YOU write: `initialize`: ; Return the first letter of a word downcase'd, followed by ".": #;(check-expect (initialize "Ian") "i.") ;; Now, let's re-factor `monogram`! #| TODO in class |# #| One reaction to factoring out `initialize` above is: "Gee Barland, you did a bunch of extra work, for a fairly small amount of duplicate-code-elimination." (a) Duplicate code smells bad; (b) Smaller functions leads to better testing-via-unit-tests; it reduces the need for printf-debugging or stepping with a debugger. (c) The helper-function might even be useful in its own right. |# ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;; Bonus full-racket code-teaser ;;;;;;;;;;;;;;;;;;;;;;;;; #| As an example of (c) above, AND a preview of the higher-order function `map` that we'll discuss later, here's another function. (You are NOT expected to be able to sit down and write this ...yet!) The following code uses the full-racket language; copy/paste the contents of the #|...|# below into a new DrRacket window, and select: Languages > Choose Language... > The Racket Language ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |# #| #lang racket (require rackunit) ; initialize: non-empty-string -> string ; Return the first letter of a word downcased, following by ".". (define (initialize wrd) (string-append (string (char-downcase (string-ref wrd 0))) ".")) (check-equal? (initialize "Ke$ha") "k.") (check-equal? (initialize "z") "z.") ; acronymize : string -> string ; Given a multi-word phrase, return its acronym. ; (define (acronymize phrase) "I'm just a stub") (check-equal? (acronymize "Self contained underwater breathing apparatus") "s.c.u.b.a.") #| ;; VERSION 2 -- satisfy the second test below, as well! (check-equal? (acronymize "Self contained underwater breathing apparatus") "s.c.u.b.a.") (check-equal? (acronymize "The light-amplified stimulated emission of radiation" #;COMMON-WORDS) "l.a.s.e.r.") ;;; Again, to be clear: ;;; In this class we will NOT use full-racket, nor worry about regexp's etc. ;;; (We will, though, use `map` in intermediate-student, in a few weeks.) |# |#