;; A LOS is one of: ;; - empty ;; - (cons String LOS) ;; LOS -> ? #;(define (los-temp an-los) (cond [(empty? an-los) ...] [(cons? an-los) ... (first an-los) ... (los-temp (rest an-los))... ])) ;; count : LOS -> Number ;; count the number of strings in given list (define (count los) (cond [(empty? los) 0] [(cons? los) (+ 1 (count (rest los)))])) (define los1 empty) (define los2 (cons "Jacob" empty)) (define los3 (cons "Sean" los2)) (define los4 (cons "Jacob" los3)) (define los5 (cons "Lauren" los4)) (check-expect (count los1) 0) (check-expect (count los2) 1) (check-expect (count los3) 2) ;; total-len : LOS -> Number ;; sum up lengths of all strings in list (define (total-len los) (cond [(empty? los) 0] [(cons? los) (+ (string-length (first los)) (total-len (rest los)))])) (check-expect (total-len los1) 0) (check-expect (total-len los2) 5) (check-expect (total-len los3) 9) ;; in? : LOS String -> Boolean ;; Is the string s in the list los? (define (in? los s) (cond [(empty? los) false] [(cons? los) (or (string=? (first los) s) (in? (rest los) s))])) (check-expect (in? los1 "Sean") false) (check-expect (in? los2 "Sean") false) (check-expect (in? los3 "Sean") true) ;; replace : LOS String String -> LOS ;; replace all occurrences of old with new in los (define (replace los old new) (cond [(empty? los) empty] [(cons? los) (cond [(string=? (first los) old) (cons new (replace (rest los) old new))] [else (cons (first los) (replace (rest los) old new))])])) (check-expect (replace los1 "Jacob" "Sean") empty) (check-expect (replace los2 "Grant" "Can") los2) (check-expect (replace los4 "Jacob" "Can") (cons "Can" (cons "Sean" (cons "Can" empty)))) (check-expect (replace los3 "Sean" "Lauren") (cons "Lauren" (cons "Jacob" empty))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; A LOS is one of: ;; - empty ;; - (cons String LOS) (define-struct customer (title first last)) ;; A Customer is a (make-customer Title String String) ;; interp. (make-customer t f l) means t is the customer's title, ;; f is the first name and l is the last name ;; A Title is one of: ;; - 'Mr. ;; - 'Ms. ;; A LOC is one of: ;; - empty ;; - (cons Customer LOC) ;; cust-temp : Customer -> ? (define (cust-temp cust) ... (customer-title cust) ... (customer-first cust) ... (customer-last cust) ...) ;; loc-temp : LOC -> ? (define (loc-temp loc) (cond [(empty? loc) ...] [(cons? loc) ... (first loc) ... (loc-temp (rest loc)) ...])) ;; letters-to-all : LOC -> LOS ;; produce a list of letters (i.e., strings) to each customer, ;; for e.g., "Dear Olin, Please come to the new store opening!" (define (letters-to-all loc) (cond [(empty? loc) empty] [(cons? loc) (cons (letter-to-one (first loc)) (letters-to-all (rest loc)))])) ;; letter-to-one : Customer -> String ;; produce a letter for the given customer (define (letter-to-one cust) (string-append "Dear " (customer-first cust) ", Please come to the new store opening!")) (define c1 (make-customer 'Mr. "Olin" "Shivers")) (define c2 (make-customer 'Ms. "Leena" "Razzaq")) (define loc1 (cons c1 empty)) (define loc2 (cons c2 loc1)) (check-expect (letters-to-all empty) empty) (check-expect (letters-to-all loc2) (cons "Dear Leena, Please come to the new store opening!" (cons "Dear Olin, Please come to the new store opening!" empty)))