;; 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 12longest) (read-case-sensitive #t) (teachpacks ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp"))))) ; DEMO: More local, more abstracting with functions ;;;;;;;; More than just clean laundry ; scrabbles : (listof string) -> (listof string) ; to produce a list of those given words that are all lower case (define (scrabble words) (local [; all-lower-case? : string -> boolean ; to tell if every character in the given string is a lowercase letter (define (all-lower-case? word) (andmap char-lower-case? (string->list word)))] (filter all-lower-case? words))) (check-expect (scrabble '("hello" "don't" "Bam!")) '("hello")) (check-expect (scrabble '()) '()) ;; (1listof X), aka non-empty list of X, is one of: ;; -- (cons X empty) ;; -- (cons X xs), where xs is a (1listof X) ;; longest : (1listof string) -> string ;; find the longest string in the list (define (longest ss) (cond [(empty? (rest ss)) (first ss)] [(cons? (rest ss)) (local [(define longest-of-rest (longest (rest ss)))] (cond [(< (string-length (first ss)) (string-length longest-of-rest)) longest-of-rest] [else (first ss)]))])) (check-expect (longest '("Call" "me" "Ishmael")) "Ishmael") (check-expect (longest '("Call" "me")) "Call") ; mostest : (X -> number) (1listof X) -> X ; (define (oldest-student students) (mostest student-age students)) ; older? : student -> boolean ; #| (time (longest (list "If" "you" "like" "you" "can" "call" "me" "Ishmael"))) (time (longest (list "My" "mother" "named" "me" "Sam" "and" "my" "father" "calls" "me" "Junior" "but" "if" "you" "like" "you" "can" "call" "me" "Ishmael"))) |# #| Related problems: - Computational biology: find the most similar protein - Games: find the highest scoring Scrabble word - Spell correction: find the most similar words Can we you do it with a single function? |#