;;; Model for Assignment 1 ;;; Ben Gum ;;; Department of Mathematics and Computer Science ;;; Grinnell College ;;; gum@cs.grinnell.edu ;;; created January 28, 2002 ;;; last revised January 28, 2002 ;;; Procedure: LENGTH computes the number of items in a list ;;; ;;; Given: LS a list ;;; ;;; Result: LEN an integer ;;; ;;; Preconditions: none ;;; ;;; Postconditions: LEN is the number of items in LS ;;; (define length (lambda (ls) (if (null? ls) 0 (+ 1 (length (cdr ls)))))) ;;; Procedure: APPEND sticks together two lists ;;; ;;; Given: LS1, LS2 lists ;;; ;;; Result: LS3 a list ;;; ;;; Preconditions: none ;;; ;;; Postconditions: LS3 contains the LS1 appended to LS2 ;;; (define append (lambda (ls1 ls2) (if (null? ls1) ls2 (cons (car ls1) (append (cdr ls1) ls2))))) ;;; Procedure: REVERSE reverses the input list ;;; ;;; Given: LS, a list ;;; ;;; Result: REV, a list ;;; ;;; Preconditions: none ;;; ;;; Postconditions: REV contains the items in LS, in reverse order ;;; (define reverse (lambda (ls) (if (null? ls) '() (append (reverse (cdr ls)) (list (car ls)))))) ;;; Procedure: LIST-REF picks a particular element out of a list ;;; ;;; Given: LS a list, N an integer ;;; ;;; Result: ITEM an item ;;; ;;; Preconditions: N >= 0 and N < the length of LS ;;; ;;; Postconditions: ITEM is the N+1st item in LS ;;; (define list-ref (lambda (ls n) (if (zero? n) (car ls) (list-ref (cdr ls) (- n 1)))))