;; 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 dist-lect06) (read-case-sensitive #t) (teachpacks ((lib "universe.ss" "teachpack" "2htdp") (lib "image.ss" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "universe.ss" "teachpack" "2htdp") (lib "image.ss" "teachpack" "2htdp"))))) #| Last time: we saw Java code that *loops* through a list, and the equivalent racket version (tail recursive, so that it doesn't build up stack frames). First-order functions: Hey, check out 'sort': sort strings alphabetically; (sort string<=? (list "hi", "bye", "Zoomba!")) sort by alphbetical-case-independent (sort (lambda (s1 s2) (string<=? (to-upcase s1) (to-upcase s2))) (list "hi", "bye", "Zoomba!")) (list "hi", "bye", "Zoomba!")) alphabetical-ignoring "A ", "The ": write filter-evens (think about: filter-odds, filter-primes, filter-long-strings, ...) write my-map (by design recipe) What about fold ? next up: ... grammars and parsing ... |# ; sort strings alphabetically; (sort (list "hi" "bye" "Zoomba!") string<=?) ; sort strings alphabetically, case-independent: ; (hint: we can use string-upcase) (sort (list "hi" "bye" "Zoomba!") (λ (s1 s2) ...)) ; sort strings, ignoring "A ", "The ": (check-expect (sort (list "A Tribe Called Quest" "The Who" "The The" "Abba") ...) (list "Abba" "The The" "A Tribe Called Quest" "The Who")) ; Does one string start with another? ; (define (has-prefix? pre str) (and (>= (string-length str) (string-length pre)) (string=? pre (substring 0 (string-length pre) str)))) ; if we used the regular-expression-library: ;(require mzlib/pregexp) ;(define (has-prefix? pre str) ; (cons? (pregexp-match (string-append "^" (pregexp-quote pre)) str))) ; Remove a prefix, if it exists. ; (define (drop-prefix-if-has pre str) (if (has-prefix? pre str) (substring str (string-length pre)) str)) (check-expect (drop-prefix-if-has "abc" "abcdef") "def") (check-expect (drop-prefix-if-has "abc" "abxdef") "abxdef") #| ; Recall: ; mapsqr : (listof number) -> (listof number) ; (define (mapsqr nums) (cond [(empty? nums) ...] [(cons? nums) ...(first nums) ...(mapsqr (rest nums))])) (check-expect (mapsqr empty) empty) (check-expect (mapsqr (cons 7 empty)) (cons 49 empty)) (check-expect (mapsqr (cons 9 (cons 7 empty))) (cons 81 (cons 49 empty))) ; Recall: move-astrs ; Or: suppose I want to take each number in a list, and multiply it by 2: ; (define (mapdouble nums) (cond [(empty? nums) ...] [(cons? nums) ...(first nums) ...(mapdouble (rest nums)))])) ; my-map : (list-of ???), ??? -> (list-of ???) ; (define (my-map data f) (cond [(empty? data) ...] [(cons? data) ...(first data) ...(my-map (rest data) f))])) (equal? (my-map (list 9 7) sqr) (list 81 49)) (equal? (my-map (list 9 7) (lambda (x) (* 2 x))) (list 18 14)) |# #| Switch over to grammars (lect06b.html) |# #| ; Generalize: write my-map: ;;;;;;;;;;;;;; ; tulfywew - the ultimate list function you will ever write ; ; Remember: (define (sum data) (cond [(empty? data) ...] [(cons? data) ...(first data) ...(sum (rest data))])) |#