;;; ;;; UVA CS200 Spring 2004 ;;; listprocs.ss ;;; 21 January 2004 ;;; ;;; Useful list procedures ;;; (define (get-tagged-list lst tag n) (if (eq? (first lst) tag) (get-nth (rest lst) n) (error (string-append "Attempt to use a non-" (symbol->string tag) " object as a " (symbol->string tag) ": ") lst))) ;; get-nth evaluates to the nth item in the list (counting first a 1)a (define (get-nth list n) (if (= n 1) (first list) (get-nth (rest list) (- n 1)))) ;;; This procedure changes the value of the nth element in the list ;;; to val. We won't cover mutation until Problem Set 5, so don't ;;; worry about how this works for now. (define (set-nth! lst n val) (if (= n 1) (set-car! lst val) (set-nth! (cdr lst) (- n 1) val))) (define (sumlist lst) (if (null? lst) 0 (+ (first lst) (sumlist (cdr lst))))) (define (intsto n) (if (= n 0) null (append (intsto (- n 1)) (list n))))