;; LISTS ;; An LOS (list of strings) is one of: ;; - empty ;; - (cons String LOS) ;; los-temp : LOS -> ? #;(define (los-temp los) (cond [(empty? los) ...] [else ... (first los) ... (los-temp (rest los)) ...])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; sum-lengths : LOS -> Number ;; Sum the lengths of all the strings in los (define (sum-lengths los) (cond [(empty? los) 0] [else (+ (string-length (first los)) (sum-lengths (rest los)))])) (check-expect (sum-lengths empty) 0) (check-expect (sum-lengths (cons "Spencer" (cons "Ameen" empty))) 12) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; in? : LOS String -> Boolean ;; Is str in los? (define (in? los str) (cond [(empty? los) false] [else (or (string=? (first los) str) (in? (rest los) str))])) (check-expect (in? empty "") false) (check-expect (in? empty "Scotty") false) (check-expect (in? (cons "Amal" empty) "Ahmed") false) (check-expect (in? (cons "Amal" empty) "Amal") true) (check-expect (in? (cons "Scotty" (cons "Sydney" empty)) "Sydney") true) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; replace : LOS String String -> LOS ;; Replace all occurrences of old in los with new (define (replace los old new) (cond [(empty? los) empty] [else (if (string=? (first los) old) (cons new (replace (rest los) old new)) (cons (first los) (replace (rest los) old new)))])) (check-expect (replace empty "Alice" "Rebecca") empty) (check-expect (replace (cons "Alice" empty) "Alice" "Rebecca") (cons "Rebecca" empty)) (check-expect (replace (cons "Jane" empty) "Alice" "Rebecca") (cons "Jane" empty)) (check-expect (replace (cons "Ben" (cons "Alice" (cons "Speedy" (cons "Alice" empty)))) "Alice" "Rebecca") (cons "Ben" (cons "Rebecca" (cons "Speedy" (cons "Rebecca" empty))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; A Posn is a (make-posn Number Number) ;; An LOP (list of posn) is one of: ;; - empty ;; - (cons Posn LOP) ;; lop-temp : LOP -> ? #;(define (lop-temp lop) (cond [(empty? lop) ...] [else ... (first lop) ... (lop-temp (rest lop)) ...])) ;; shift-posns : LOP -> LOP ;; shift all posns in lop by adding 10 to x coord of each posn (define (shift-posns lop) (cond [(empty? lop) empty] [else (cons (shift-posn (first lop)) (shift-posns (rest lop)))])) ;; Wishful thinking ;; shift-posn : Posn -> Posn ;; shift posn p by adding 10 to x coord (define (shift-posn p) (make-posn (+ 10 (posn-x p)) (posn-y p))) (check-expect (shift-posn (make-posn 0 0)) (make-posn 10 0)) ;;; examples for shift-posns (check-expect (shift-posns empty) empty) (check-expect (shift-posns (cons (make-posn 0 0) empty)) (cons (make-posn 10 0) empty)) (check-expect (shift-posns (cons (make-posn 0 0) (cons (make-posn 40 2) empty))) (cons (make-posn 10 0) (cons (make-posn 50 2) empty)))