;; 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-reader.ss" "lang")((modname list-practice-map-filter) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) ; functions that do a map ; writing map ourselves -- pass in a function as input ; map a fuction which returns a boolean ; filter -- another higher-order function ; see an example of making one of these tail-recursive ; php: see `array_map` ;;; data definition: ; a list-of-strings is: ; - '(), OR ; - (cons [string] [list-of-strings]) ; examples of the data: '() (cons "google" '()) (consl "the" (cons "google" '())) (cons "bing" (cons "the" (cons "google" '()))) ; template, for any function handling a list-of-strings: ; handle-strings: list-of-strings -> ??? (define (handle-strings strs) (cond [(empty? strs) (...)] [(cons? strs) (... (first strs) ... (handle-strings (rest strs)) ...)])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Given a list-of-strings, return a list of the length of each string in the list. (check-expect (lengths '()) '()) (check-expect (lengths (cons "google" '())) (cons 6 '())) (check-expect (lengths (cons "the" (cons "google" '()))) (cons 3 (cons 6 '()))) (check-expect (lengths (cons "bing" (cons "the" (cons "google" '())))) (cons 4 (cons 3 (cons 6 '())))) ; lengths : list-of-strings -> list-of-natnum ; A list of the length of each string in strs. #;(define (lengths strs) (cond [(empty? strs) '()] [(cons? strs) (cons (string-length (first strs)) (lengths (rest strs)))])) ;;;;;;;; ; Given a list-of-strings, return a list of the first letter (or, `false` if it is the empty-string). (check-expect (initials '()) '()) (check-expect (initials (cons "google" '())) (cons #\g '())) (check-expect (initials (cons "" '())) (cons false '())) (check-expect (initials (cons "the" (cons "google" '()))) (cons #\t (cons #\g '()))) (check-expect (initials (cons "bing" (cons "the" (cons "google" '())))) (cons #\b (cons #\t (cons #\g '())))) (check-expect (initials (cons "bing" (cons "" (cons "google" '())))) (cons #\b (cons false (cons #\g '())))) ; initials : list-of-strings -> list-of-(char-or-false) #;(define (initials strs) (cond [(empty? strs) '()] [(cons? strs) (cons (initial (first strs)) (initials (rest strs)))])) ; initial : string -> char OR false (define (initial wrd) (if (string=? wrd "") false (string-ref wrd 0))) (check-expect (initial "google") #\g) (check-expect (initial "") false) (check-expect (initial "z") #\z) ;;; re-factor initials and lengths: ; my-map : list-of-α, (α -> β) -> list-of-β (define (my-map strs hammer) (cond [(empty? strs) '()] [(cons? strs) (cons (hammer (first strs)) (my-map (rest strs) hammer))])) (define (initials strs) (my-map strs initial)) (define (lengths strs) (my-map strs string-length)) ;; in real code, now that we have 'my-map' (or built-in: 'map`), ;; programmers don't take the extra step of defining `initials` or `lengths`; ;; they just call `map` and pass in `string-length` or `initial`. ;; ;; ...(my-map strs string-length)... ;; this is in Java as .. java.util.stream.Collectors.map (I think) ;; NOT as the hash-table java.util.Map ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; take a list of strings, and return a list of booleans: ;; Does the string start with an upper-case letter? (check-expect (my-map (cons "bing" (cons "the" (cons "Google" '()))) start-upcase?) (cons false (cons false (cons true '())))) ; start-upcase? : string -> boolean ; (define (start-upcase? str) (if (string=? str "") false (char-upper-case? (string-ref str 0)))) (check-expect (start-upcase? "Hi") true) (check-expect (start-upcase? "hi") false) (check-expect (start-upcase? "") false) ;; Given a list of strings, return a list of those strings which start with an upper-case letter. ; keep-upcase : list-of-string -> list-of-string ;;; EXERCISE: write this function yourself. ;;; HINT: as a helper, you'll call `start-upcase?` #;(check-expect (keep-upcase (cons "Google" (cons "the" (cons "Bing" (cons "crosby" (cons "!!!" '())))))) (cons "Google" (cons "Bing" '()))) ;Funcionts like `keep-upcase` are another common idiom: ; filter (check-expect (filter start-upcase? (cons "Google" (cons "the" (cons "Bing" (cons "crosby" (cons "!!!" '())))))) (cons "Google" (cons "Bing" '())))