;; 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 13mostest) (read-case-sensitive #t) (teachpacks ((lib "image.rkt" "teachpack" "2htdp") (lib "universe.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.rkt" "teachpack" "2htdp") (lib "universe.rkt" "teachpack" "2htdp"))))) ; DEMO: list functions: mostest, foldr, how-many; lambda ; scrabble-words : (listof string) -> (listof string) ; to produce the words on the given list that are all lowercase letters (define (scrabble-words ws) (filter (lambda (w) (andmap char-lower-case? (string->list w))) ws)) (check-expect (scrabble-words '("hello" "don't" "Bam!")) '("hello")) (check-expect (scrabble-words '()) '()) ;; (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 ; return the highest ranking of the given list of Xs (define (mostest rank ne-lox) (cond [(empty? (rest ne-lox)) (first ne-lox)] [(cons? (rest ne-lox)) (local [(define mostest-of-rest (mostest rank (rest ne-lox)))] (cond [(> (rank (first ne-lox)) (rank mostest-of-rest)) (first ne-lox)] [else mostest-of-rest]))])) (check-expect (mostest string-length '("Call" "me" "Ishmael")) "Ishmael") (check-expect (mostest sqr '(1 2 3 4 -10)) -10) (check-expect (mostest abs '(-10 2 4 9)) -10) (define-struct person (first last)) (define people (list (make-person "Donovan" "Snyder") (make-person "Norman" "Ramsey"))) (check-expect (mostest (lambda (p) (string-length (person-first p))) people) (make-person "Donovan" "Snyder")) (define (oldest-student students) (mostest student-age students)) (define-struct student (name age height country)) #| Related problems: - Class: find the oldest student - Computational biology: find the most similar protein - Games: find the highest scoring Scrabble word - Spell correction: find the most similar words - Fashion: find the Red Sox player with the longest beard |# #| Combination: - Combine numbers to form the sum - Combine numbers to form the product - Combine images (how many ways)? - Combine strings with spaces between - Purpose statements that begin "How many?" |# ; sum : (listof number) -> number ; to return the sum of the numbers on the given list (define (sum ns) (cond [(empty? ns) 0] [(cons? ns) (+ (first ns) (sum (rest ns)))])) (check-expect (sum '()) 0) (check-expect (sum '(1 2 3 4)) 10) ; product : (listof number) -> number ; to return the produce of the numbers on the given list (define (product ns) (cond [(empty? ns) 1] [(cons? ns) (* (first ns) (product (rest ns)))])) (check-expect (product '()) 1) (check-expect (product '(1 2 3 4)) 24) ; my-foldr : ( X Y -> Y) Y (listof X) -> Y ; to combine all the given elements with 'for-empty' using 'combine' (define (my-foldr combine for-empty lox) (cond [(empty? lox) for-empty] [(cons? lox) (combine (first lox) (my-foldr combine for-empty (rest lox)))])) (check-expect (my-foldr + 0 '(1 2 3 4)) 10) (check-expect (my-foldr * 1 '(2 2 2 2)) 16) (check-expect (my-foldr string-append "" '("Hello" "all" "students")) "Helloallstudents") (check-expect (my-foldr (lambda (left right) (string-append left " " right)) "" '("Hello" "all" "students")) "Hello all students ") ; how-many : (X -> boolean) (listof X) -> number ; to tell how many elements of the given list satisfy p? (define (how-many p? lox) (foldr (lambda (x total) (+ (cond [(p? x) 1] [else 0]) total)) 0 lox)) (check-expect (how-many positive? '(0 1 2)) 2) (check-expect (how-many (lambda (s) (> (string-length s) 4)) '("Norman" "Donovan" "Ali" "Cori" "Boris")) 3) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define c1 (circle 7 "solid" "red")) (define c2 (circle 9 "solid" "black")) (define s1 (square 12 "solid" "blue")) (define t1 (triangle 20 "solid" "red")) (define t2 (triangle 16 "outline" "blue")) (define images (list c1 s1 t1 t2 c2))