;;; File: ;;; sum.ss ;;; Author: ;;; Samuel A. Rebelsky ;;; Version: ;;; 1.0 of April 2001 ;;; Contents: ;;; all-numbers? - verify that all the values in a list are numbers ;;; suml - sum the values in a list ;;; sumr - sum the values in a list ;;; History: ;;; Tuesday, 3 April 2001 ;;; Created because students in CSC151 had not kept their notes ;;; on the subject. ;;; Procedure: ;;; all-numbers? ;;; Parameters: ;;; lst, a list ;;; Purpose: ;;; Determine whether all the values in a list are numbers. ;;; Produces: ;;; ok, a Boolean value (#t or #f) ;;; Preconditions: ;;; lst is a list [Unverified] ;;; Postconditions: ;;; ok is #f if lst contains a non-number. ;;; ok is #t otherwise. ;;; Note that this means that ok is #t if lst is empty. (define all-numbers? (lambda (lst) (or ; The empty list contains no non numbers (null? lst) ; The first element must be a number and the rest ; must contain only numbers (and (number? (car lst)) (all-numbers? (cdr lst)))))) ;;; Procedures: ;;; suml ;;; sumr ;;; Parameters: ;;; numbers, a list of numbers ;;; Purpose: ;;; Sum all the values in numbers. ;;; Produces: ;;; sum, a number ;;; Preconditions: ;;; numbers is a list. [Verified] ;;; All the values in numbers are numbers. [Verified] ;;; Scheme is able to represent the sum of all the values. [Unverified] ;;; Postconditions: ;;; sum is the sum of the values in numbers. ;;; Note: ;;; suml sums from left to right (adding the first two numbers, then ;;; adding the next number, then adding the next number, and so on ;;; so forth). CSC151.2001S wrote a version of this in class. ;;; sumr sums from right to left (adding the last two numbers, then ;;; adding the preceding number, and so on and so forth). CSC151.2001S ;;; saw a version of this in a handout. ;;; Examples: ;;; (suml ()) ;;; => 0 ;;; (suml (list 1 2 3)) ;;; => 6 (define suml (lambda (numbers) (cond ; Verify precondition: numbers is a list. ((not (list? numbers)) (error "suml" "parameter is not a list")) ; Verify precondition: numbers contains only numbers. ((not (all-numbers? numbers)) (error "suml" "parameter contains non-numbers")) ; Preconditions are okay, ready to go (else ; (kernel partial-sum remaining-numbers) ; add partial-sum to the sum of remaining-numbers ; partial-sum ives the sum of the elements processed so far. ; remaining-numbers gives the remaining elements in the list. (let kernel ((partial-sum 0) (remaining-numbers numbers)) ; If no numbers remain, the partial sum is the complete ; sum. (if (null? remaining-numbers) partial-sum ; Otherwise, add the next number to the sum and ; continue with the rest (kernel (+ partial-sum (car remaining-numbers)) (cdr remaining-numbers)))))))) (define sumr (lambda (numbers) (cond ; Verify precondition: numbers is a list. ((not (list? numbers)) (error "sumr" "parameter is not a list")) ; Verify precondition: numbers contains only numbers. ((not (all-numbers? numbers)) (error "sumr" "parameter contains non-numbers")) ; Preconditions are okay, ready to go. (else ; (kernel some-numbers): sum some-numbers without ; checking preconditions (let kernel ((some-numbers numbers)) ; If no numbers remain, the sum is 0 (if (null? some-numbers) 0 ; Otherwise, add the first number ; to the sum of the remaining numbers. (+ (car some-numbers) (kernel (cdr some-numbers)))))))))