;; 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" strs0)) (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 string) -> (list-of natnum) ; map `string-length` to `strs`. ; (define (map-string-length strs) (cond [(empty? strs) empty] [(cons? strs) (cons (string-length (first strs)) (map-string-length (rest strs)))])) ; map-initialize : (list-of string) -> (list-of string) ; map `initialize` to `strs`.c ; #;(define (map-initialize strs) (cond [(empty? strs) empty] [(cons? strs) (cons (initialize (first strs)) (map-initialize (rest strs)))])) (define (map-initialize strs) (my-map initialize strs)) (check-expect (map-initialize strs3) (cons "c." (cons "g." (cons "h." '())))) (define string-upcase (λ (s) (list->string (map char-upcase (string->list s))))) (define (map-string-upcase-v1 strs) (map (λ (s) (list->string (map char-upcase (string->list s)))) strs)) (define (map-string-upcase-v2 strs) (map string-upcase strs)) (define map-string-upcase map-string-upcase-v2) ;;; Use `upcase-char`, `string->list`, and `list->string` (check-expect (map-string-upcase strs3) (cons "CIAO" (cons "GUTEN TAG" (cons "HELLO" '())))) ; Given a list of strings, here's a list of all the second-halves: ; ;;;; Enough already! Let's just write `my-map`: ; my-map: (alpha -> beta), list-of-alpha -> list-of-beta ; given a list of `things`, return a list of ; the results of applying `f` to each item in `things` (define (my-map f things) (cond [(empty? things) empty] [(cons? things) (cons (f (first things)) (my-map f (rest things)))])) ; Now, go back and re-write