;; 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 lect04c-v2) (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"))))) #| hw04 discuss: draw-astrs by template; then, as tail-recursive design recipe: http://htdp.org/2003-09-26/Book/curriculum-Z-H-13.html#node_sec_9.4 tail-recursion; using an accumulator. Exercise: write 'min' using an accumulator; first in Java, then in tail-recursive racket review/remember: lambda ;;;;;;;;;; for next week: 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-igorning "A ", "The ": write filter-evens (think about: filter-odds, filter-primes, filter-long-strings, ...) write my-map (by design recipe) What about fold ? ... grammars and parsing ... |# (require mzlib/pregexp) (define (has-prefix? pre str) (cons? (pregexp-match (string-append "^" pre) str))) (define (drop-prefix pre str) (substring str (string-length pre))) (check-expect (drop-prefix "abc" "abcdef") "def") (define (help-min_v2 data min-so-far) (cond [(empty? data) min-so-far] [(cons? data) (min_v2 (rest data) (min-of-two (first data) min-so-far))])) ; min_v2 : cons? -> number (define (min_v2 nums) (help-min_v2 (rest nums) (first nums))) (define (min-of-two x y) (if (< x y) x y))