;; 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) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ; Give the racket version of the math-expression: (1+√5)/2 (/ (+ 1 (sqrt 5)) 2) #| Examples of primitive data: number 34 34.5 #i3.14 string "hello" char #\A #\space boolean #true #false symbol 'green 'read-only (and regexp #rx"[0-9]*", images, ports, etc) |# #| Recall: some built-in functions and their signatures (types): Signature Example substring : string, natnum, natnum -> string (substring "hello" 1 4) = "ell" string-ref : (string-ref "hello" 1) = #\e string-length : string -> natnum (string-length "hello") = 5 string-append : (string-append "hel" "lo") = "hello" = : number, number -> boolean (= 4 (+ 2 3)) = #false string=? : (string=? "ell" (substring "hello" 1 4)) = #true string? : ANY -> boolean (string? 43) = #false number->string : number -> string (number->string (* 7 6)) = "42" char-downcase : (char-downcase #\A) = #\a |# #| Can use 'define' to bind an identifier to a value: |# (define n 37) (define m (* n 99)) (define fname "Ian") (define lname "B-meister") #| The Design Recipe (take 1 -- primitive types only) ------- per function: 4. tests 5. stub : signature, description, head (with empty body) 7. complete the body-expression 8. watch your tests pass |# ;; EXAMPLE: triplify ; triplify : number -> number ; Given a number `x`, triple it. (define (triplify x) (* 3 x)) (check-expect (triplify 5) 15) (check-expect (triplify 2437.2537) 7311.7611) (check-expect (triplify 0) 0) (check-expect (triplify -2) -6) (check-expect (triplify 2.5) 7.5) (check-expect (triplify (/ 1 3)) 1) ; New syntax: `define` for functions. ; Note how it mirrors how the function is called (but params instead of argument-expressions). ;;; EXAMPLE: Together, we'll write: ; monogram: given a first name and last name, return a monogram with initials: ; monogram : string, string -> string ; Return the first and last initials. ; `first-name`, `last-name` Cannot be the empty string. (define (monogram first-name last-name) (string (char-downcase (string-ref first-name 0)) #\. (char-downcase (string-ref last-name 0)) #\.)) (check-expect (monogram "Ian" "Barland") "i.b.") (check-expect (monogram "joey" "robbinz") "j.r.") (check-expect (monogram "Jay" "Z") "j.z.") (check-expect (monogram "F" "Fitzgerald") "f.f.") (check-expect (monogram "!eric" "?huh") "!.?.") ; Note -- you're not expected to know about `string-ref`, `char-downcase`, `string` -- ; those are things you'd learn, when you need them, scouring through the documentation. ;;; EXAMPLE: Okay, now YOU write: `initialize`: ; Return the first letter of a word downcased, following by ".". (check-expect (initialize "Hello") "h.") (check-expect (initialize "hello") "h.") (check-expect (initialize "A") "a.") (check-expect (initialize "#") "#.") ; initialize : string -> string ; return the first letter of a word downcased, following by ".". (define (initialize wrd) (string (char-downcase (string-ref wrd 0)) #\.)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; A Re-factored version of `monogram`, once we have `initialize`: #;(define (monogram first-name last-name) (string-append (initialize first-name) (initialize last-name)))