;; 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 11hof) (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 ;; (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)) (cond [(< (string-length (first ss)) (string-length (longest (rest ss)))) (longest (rest ss))] [else (first ss)])])) (check-expect (longest '("Call" "me" "Ishmael")) "Ishmael") (check-expect (longest '("Call" "me")) "Call") #| (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? |# ; A *student* is a kind of data not specified ; carries-MacBook? : student -> boolean ; to tell if a student carries a MacBook (define (carries-MacBook? student) ...) ; lives-in-Tilton? : student -> boolean ; to tell if a student lives in Tilton (define (lives-in-Tilton? student) ...) ; alltilton? : (listof student) -> boolean ; tell if all students on the given list live in Tilton (define (alltilton? los) (cond [(empty? los) true] ;; could be false; tests will know [(cons? los) (and (lives-in-Tilton? (first los)) (alltilton? (rest los)))])) ; allmac? : (listof student) -> boolean ; tell if all students on the given list carry MacBooks (define (allmac? los) (cond [(empty? los) true] [(cons? los) (and (carries-MacBook? (first los)) (allmac? (rest los)))])) ; all? : (X -> boolean) (listof X) -> boolean ; tell if all elements on the given list satisfy 'helper?' (define (all? helper? los) (cond [(empty? los) true] [(cons? los) (and (helper? (first los)) (all? helper? (rest los)))])) (define (simpler-allmac? los) (all? carries-MacBook? los)) (define (simpler-alltilton? los) (all? lives-in-Tilton? los)) ; has-Android-phone? : student -> boolean ; to tell if a student has an Android phone (define (has-Android? student) ...) ; some-student-carries-MB? : (listof student) -> boolean ; to tell if some student on the given list carries a MacBook (define (some-student-carries-MB? ss) ...) ; some-student-lives-in-Tilton? : (listof student) -> boolean ; to tell if some student on the given list lives in Tilton (define (some-student-lives-in-T? ss) ...) ; some-student-has-A? : (listof student) -> boolean ; to tell if some student on the given list has an Android phone (define (some-student-has-A? ss) ...) ;;;; eliminate similarities ;;; going straight for the jugular! ;;; To tell if all ... ;;; To produce a list of all ...