;; sum : list-of-number -> number ;; produces sum of numbers in the list (define (sum alon) (cond [(empty? alon) 0] [(cons? alon) (+ (first alon) (sum (rest alon)))])) ;; sum-accum : list-of-number number -> number ;; produces sum of numbers in the list ;; (produces sum of numbers in a list with the result so far ;; recorded in a given (number) input) (define (sum-accum alon total) (cond [(empty? alon) total] [(cons? alon) (sum-accum (rest alon) (+ (first alon) total))])) ;; sum2 : list-of-number -> number <---exactly same contract and ;; produces sum of numbers in the list purpose as old program (define (sum2 alon) (sum-accum alon 0)) ;; long-words : list-of-string -> list-of-string ;; produce list of strings from input list containing at least 5 characters (define (long-words alos) (long-words-accum alos empty)) ;; long-words-accum : list-of-string list-of-string -> list-of-string ;; accumulate long strings from first list in second list (define (long-words-accum alos list-of-long) (cond [(empty? alos) list-of-long] [(cons? alos) (cond [(> (string-length (first alos)) 5) (long-words-accum (rest alos) (cons (first alos) list-of-long))] [else (long-words-accum (rest alos) list-of-long)])])) ;; revers : list-of-string -> list-of-string ;; produce list of string with strings in opposite order from given list (define (revers alos) (rev-accum alos empty)) ;; rev-accum : list-of-string list-of-string -> list-of-string ;; accumulates reverse of first list in second list (define (rev-accum alos revalos) (cond [(empty? alos) revalos] [(cons? alos) (rev-accum (rest alos) (cons (first alos) revalos))])) ;; reverse2 : list-of-string -> list-of-string ;; produce list of string with strings in opposite order from given list (define (reverse2 alos) (cond [(empty? alos) empty] [(cons? alos) (append (reverse2 (rest alos)) (list (first alos)))]))