;; 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). Question on hw: how to reverse, accumulator-style? In Java: /* Return a new list, like `data` but with all elements reversed. */ List reverse_v2( List data ) { List reversalSoFar = new ArrayList(); while (!data.isEmpty()) { reversalSoFar.add(0, data.get(0)); data = data.subList(1,data.size()); } return reversalSoFar; } 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 ... |# #| ; asteroid boolean ; (define (asteroidlist s)))) ; 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) (string= (string-length str) (string-length pre)) (string=? pre (substring str 0 (string-length pre))))) ; 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") ; sort strings, ignoring "A ", "The ": (sort (list "A Tribe Called Quest" "The Who" "The The" "Abba") (lambda (s1 s2) (string (listof number) ; (define (mapsqr nums) (cond [(empty? nums) empty] [(cons? nums) (cons (sqr (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) empty] [(cons? nums) (cons (* 2 (first nums)) (mapdouble (rest nums)))])) ; my-map : (list-of alpha), (alpha -> beta) -> (list-of beta) ; (define (my-map data f) (cond [(empty? data) empty] [(cons? data) (cons (f (first data)) (my-map (rest data) f))])) ; alpha,beta are "type variables"; they stand for *type* ; (and is not itself code, in Racket) #| ; Are there type-variables in Java? Yes! ; class Apple implements comparable { int compareTo( T that ) { ... } } |# (equal? (my-map (list 9 7) sqr) (list 81 49)) (equal? (my-map (list 9 7) (lambda (x) (* 2 x))) (list 18 14)) ; my-map is a "higher-order function" -- it is a ; function that takes another function as input. ; (Btw, in math class they did this too: ; the "integral" is a function that takes ; in a function, and returns another function.) ; Racket has "first class functions" -- that is, ; functions are values, and can passed in to functions, ; or returned from functions, as any other value can be. #| 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))])) |#