;; 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 lect04) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) #| Recall: Examples of primitive data: number 34 34.5 #i3.14 string "hello" char #\A #\space boolean #t #f symbol 'green 'read-only (...regexp #rx"[0-9]*", images, ports, ...) |# #| Recall: some built-in functions and their signatures (types): Signature Example substring : string, natnum, natnum -> string (substring "hello" 1 4) = "ell" string-ref : string, natnum -> char (string-ref "hello" 1) = #\e string-length : string -> natnum (string-length "hello") = 5 string-append : string, string -> string (string-append "hel" "lo") = "hello" = : number, number -> boolean (= 4 (+ 2 3)) = false string=? : string, string -> boolean (string=? "ell" (substring "hello" 1 4)) = true string? : ANY -> boolean (string? 43) = false |# #| Can use 'define' to bind an identifier to a value: |# (define n 37) (define name "Ian B-meister") ; Note the following are identifers: `false`; `+` #| The Design Recipe (take 1 -- primitive types only) ------- per function: 4. tests 5. stub : signature, head, description, body 7. complete the body-expression 8. watch your tests pass |# ; triplify : number -> number ; Triple a given number. ; (define (triplify n) (* n 3)) (check-expect (triplify 5) 15) (check-expect (triplify -1.5) -4.5) ; New syntax: `define` for functions. ; Note how it mirrors how the function is called (but params instead of argument-expressions). (check-expect (triplify 0) 0) ; You: write: ; monogram: given a first name and last name, return a monogram with initials: ; (monogram "Ian" "Barland") -> "i.b." (check-expect (monogram "Ian" "Barland") "i.b.") (check-expect (monogram "Jay" "Z") "j.z.") ; monogram : string, string -> string ; Construct a monogram from the first initials of `first-name`, `last-name` names. ; `first-name` and `last-name` must be non-empty. ; (define (monogram first-name last-name) (string-append (string (char-downcase (string-ref first-name 0))) "." (string (char-downcase (string-ref last-name 0))) ".")) ; 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. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; A Re-factored version, once we have `initialize`: (define (monogram first-name last-name) (string-append (initialize first-name) (initialize last-name))) ; initialize : string -> string ; Return the first letter of `wrd` downcased, following by ".". ; (define (initialize wrd) (string (char-downcase (string-ref wrd 0)) #\.)) (check-expect (initialize "hello") "h.") (check-expect (initialize "X") "x.") ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; #| ; In full racket -- just as a teaser: ; Once we have `initialize` making acronyms not so bad: ; acronymize : string -> string ; Return an acronym of `str`: the first initial of each word. ; (define (acronymize str) (apply string-append (map initialize (regexp-split #px"\\s-" str)))) (module+ test (require rackunit) (check-equal? (acronymize "Light-amplified stimulated emisssion radiation") "l.a.s.e.r.") (check-equal? (acronymize "Radio and detecting and ranging") "r.a.d.a.r.") ;also, 'sonar'. (check-equal? (acronymize "self-contained underwater breathing apparatus") "s.c.u.b.a.") ) |#