Rewrite the following function into accumulator style. Identify the accumulator invariant.

;; all-true? : (listof boolean) -> boolean
(define (all-true? lob)
  (cond
    [(empty? lob) true]
    [else (and (first lob)
               (all-true? (rest lob)))]))

Solution

(define (all-true? lob)
  (all-true?/a lob true))

;; all-true?/a : (listof boolean) boolean -> boolean
;; the accumulator indicates if we have not seen any falses so far
(define (all-true?/a lob a)
  (cond
    [(empty? lob) a]
    [else (all-true? (rest lob)
                     (and (first lob) a))]))