Approximate overview
Design and write recursive functions over lists.
Write a recursive procedure, (increasing-length? words), that takes
a list of strings as input and ensures that every string is at least as
long as the previous string. If so, it returns true. If not, it returns
false.
Here’s a partial test suite.
(check-equal? (increasing-length '())
#t
"No strings: They are in increasing length")
(check-equal? (increasing-length? '("hello"))
#t
"A singleton")
(check-equal? (increasing-length? '("a" "b" "cd" "efg" "hij" "klmn"))
#t
"Some duplicate-length words")
(check-equal? (increasing-length? '("a" "bb" "ccc" "dddd" "eee"))
#f
"Okay until the end.")
You’ve seen a few common patterns of list recursion.
It’s probably helpful to have those patterns written down somewhere. (Hint hint hint.)
Notes from Sam’s observations on Monday.
Start with straightforward tests. Edge cases should be edgy primarily in that they are at the “edges” of possible input (e.g., the value of interest is at the front or end of the list).
(my-length '( )) is no different than (my-length '())(my-length '("hello" (list 1 2))) should be no different than
(my-length '(me 2))(test-equal? "long list" (my-length (make-list 1000 'a)) 1000)tally-oddSo many options!
(define tally-odd-a
(lambda (numbers)
(cond
[(null? numbers)
0]
[(odd? (car numbers))
(+ 1 (tally-odd-a (cdr numbers)))]
[else
(tally-odd-a (cdr numbers))])))
(define tally-odd-b
(lambda (numbers)
(cond
[(null? numbers)
0]
[(even? (car numbers))
(tally-odd-b (cdr numbers))]
[else
(+ 1 (tally-odd-b (cdr numbers)))])))
(define tally-odd-c
(lambda (numbers)
(if (null? numbers)
0
(if (odd? (car numbers))
(+ 1 (tally-odd-c (cdr numbers)))
(tally-odd-c (cdr numbers))))))
(define tally-odd-d
(lambda (numbers)
(if (null? numbers)
0
(+ (if (odd? (car numbers)) 1 0)
(tally-odd-d (cdr numbers))))))
(define tally-odd-e
(lambda (numbers)
(if (null? numbers)
0
(+ (abs (remainder (car numbers) 2))
(tally-odd-e (cdr numbers))))))
(define tally-odd-f
(lambda (numbers)
(if (null? numbers)
0
(+ (modulo (car numbers) 2)
(tally-odd-f (cdr numbers))))))