;; 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-beginner-reader.ss" "lang")((modname M06_01) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor mixed-fraction #f #t none #f () #t)))
;; (total-string-length los) produce the total length of all\
;; strings in los
;; Examples:
(check-expect (total-string-length empty) 0)
(check-expect (total-string-length (cons "dog" (cons "cat" empty))) 6)
;; total-string-length: (listof Str) -> Nat
(define (total-string-length los)
(cond [(empty? los) 0]
[(cons? los) (+ (string-length (first los))
(total-string-length (rest los)))]))
;; Tests:
(check-expect (total-string-length (cons "" (cons "cat" empty))) 3)
(check-expect (total-string-length (cons "" (cons "" empty))) 0)
(check-expect (total-string-length (cons "" empty)) 0)
;; (my-length loa) produces the number of elements of loa
;; Examples:
;(check-expect (my-length empty) 0)
;(check-expect (my-length (cons 1 (cons 2 empty))) 2)
;; my-length: (listof Any) -> Nat
(define (my-length loa)
(cond [(empty? loa) 0]
[(cons? loa) (+ 1 (my-length (rest loa)))]))
;; (count-str s los) produce the number of occurrences of s
;; in los
;; Example:
(check-expect (count-str "dog" empty) 0)
(check-expect (count-str "dog" (cons "dog" (cons "cat" empty))) 1)
;; count-str: Str (listof Str) -> Nat
(define (count-str s los)
(cond [(empty? los) 0]
[(string=? s (first los))
(+ 1 (count-str s (rest los)))]
[else (count-str s (rest los))]))
;; Tests
(check-expect (count-str "bird" (cons "dog" (cons "cat" empty))) 0)
(check-expect (count-str "" (cons "dog" (cons "cat" empty))) 0)
(check-expect (count-str "" (cons "" (cons "cat" empty))) 1)
;; (add1-list lon) produces the list of numbers in lon
;; incremented by one
;; Examples:
(check-expect (add1-list empty) empty)
(check-expect (add1-list (cons 1 (cons 2 empty))) (cons 2 (cons 3 empty)))
;; add1-list: (listof Nat) -> (listof Nat)
(define (add1-list lon)
(cond
[(empty? lon) empty]
[else (cons (+ 1 (first lon))
(add1-list (rest lon)))]))
;; We can also do simple recursion for other data definitions. For example,
;; this is a list that can only have even length. When we write a template
;; for it, notice that we remove two elements at a time. This is still
;; simple recursion, because the data definition suggests that we must
;; remove two elements to get to a smaller (even-listof X)
;; A (even-listof X) is one of:
;; ⋆ empty
;; ⋆ (cons X (cons X (even-listof X)))
;; even-listof-X-template: (even-listof X) → Any
(define (even-listof-X-template elox)
(cond [(empty? elox) ...]
[(cons? elox) (... (first elox)
(first (rest elox))
(even-listof-X-template (rest (rest elox))))]))
;; (even-count eloa) produces the number of elements in eloa
;; Examples:
(check-expect (even-count empty) 0)
(check-expect (even-count (cons 1 (cons 2 empty))) 2)
;; even-count: (even-listof Any) -> Nat
(define (even-count eloa)
(cond [(empty? eloa) 0]
[else (+ 2 (even-count (rest (rest eloa))))]))