;; 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-intermediate-lambda-reader.ss" "lang")((modname lect22-my-map) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ;;;;;;;; taken from earlier lectures, as a sample ;;;;;;;;;;; (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))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; "mapping" a function means: ; apply the function to each element of a list, ; getting back a list of the results. ; (define strs0 '()) (define strs1 (cons "hello" '())) (define strs2 (cons "Guten Tag" strs1)) (define strs3 (cons "ciao" strs2)) ;; strs3 is '("ciao" "Guten Tag" "hello") (check-expect (map-string-length strs3) (cons 4 (cons 9 (cons 5 '())))) (check-expect (map-string-length strs0) '()) (check-expect (map-string-length strs2) (cons 9 (cons 5 '()))) ;;; data-defn: ; A list-of-strings is: ; - '(), OR ; - (cons [string] [list-of-strings]) ; map-string-length : list-of-strings -> list-of-numbers ; map `string-length` to `strs`. #;(define (map-string-length strs) (cond [(empty? strs) '()] [(cons? strs) (cons (string-length (first strs)) (map-string-length (rest strs)))])) (define (map-string-length strs) (my-map string-length strs)) ; map-initialize : list-of-strings -> list-of-string ; map `initialize` to `strs`. ; (define (map-initialize strs) (cond [(empty? strs) '()] [(cons? strs) (cons (initialize (first strs)) (map-initialize (rest strs)))])) (check-expect (map-initialize strs3) (cons "c." (cons "g." (cons "h." '())))) ; Given a list of strings, here's a list of all the second-halves: ;;; recall: #;(define (triplify n) (+ n n n)) ; is syntactic sugar for: (define triplify (lambda (n) (+ n n n))) ;;; In java8: `(n) -> { return n+n+n; }` or even just `n -> n+n+n` ;;; (In java, that is syntactic sugar for creating an anonymous ;;; class, with one named method, where the above lambda-expr ;;; turns into that named method.) ;;; In javascript: ;;; `function (n) { return n+n+n; }` for the anon function; ;;; `var triplify = function (n) { return n+n+n; };` ; command-backslash gives 'λ' ;;;; Enough already! Let's just write `my-map`: ; my-map: ; Return a list of results of calling `f` on every element of `inputs`. ; (define (my-map f inputs) (cond [(empty? inputs) '()] [(cons? inputs) (cons (f (first inputs)) (my-map f (rest inputs)))])) (check-expect (my-map initialize strs3) (map-initialize strs3)) ; Now, go back and re-write (check-expect (my-map (λ (str) (substring str (quotient (string-length str) 2))) strs3) (cons "ao" (cons "n Tag" (cons "llo" '())))) ; what about taking strings, and upcasing each them? ; Problem: upcase-string not in beginning-student! ; Solution: we'll write it ourselves, using ; string->list : string -> list-of-chars ; list->string : list-of-chars -> string ; char-upcase : char -> char (check-expect (my-map (λ (str) (list->string (my-map char-upcase (string->list str)))) strs3) (cons "CIAO" (cons "GUTEN TAG" (cons "HELLO" '())))) ; `my-map` is actually built-in, as `map`. (map string->number (list "43" "000" "+2.30000"))