;; 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-dist) (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, ...) |# (string=? "ell" (substring "hello" 1 4)) (string=? "ell" "ell") #t #| 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)) = #t string? : ANY -> boolean (string? 43) = false This notation jibes with math: f : R -> R |# #| Can use 'define' to bind an identifier to a value: |# (define n 37) (define name "Ian B-meister") ; built-in somewhere: (define pi #i3.1415926535) ; Note the following are identifers: `false`; `+` #| The Design Recipe (take 1 -- primitive types only) ------- per function: 4. tests 5. stub : signature, head, description, stub-body 7. complete the body-expression 8. watch your tests pass |# ; triplify : number -> number ; (define (triplify n) (* 3 n)) (check-expect (triplify 0) 0) (check-expect (triplify 1) 3) (check-expect (triplify 4) 12) (check-expect (triplify -2.5) -7.5) ; New syntax: `define` for functions. ; Note how it mirrors how the function is called (but params instead of argument-expressions). ; You: write: ; monogram: given a first name and last name, return a monogram with initials: ; (monogram "Ian" "Barland") -> "i.b." ; monogram : string string -> string ; Given `first-name` and `last-name`, return a monogram with initials: ; `first-name` and `last-name` must be non-empty strings. ; (define (monogram first-name last-name) (string-append (string (char-downcase (string-ref first-name 0))) "." (string (char-downcase (string-ref last-name 0))) ".")) (check-expect (monogram "Ian" "Barland") "i.b.") (check-expect (monogram "A" "Z") "a.z.") (check-expect (monogram "Jay" "Z") "j.z.") (check-expect (monogram "Cruella" "deVille") "c.d.") #| ; 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.") ) |#