(define-class 'stack object-class '(num-items item-vector) '(push pop top height empty? display)) (class/set-method! stack-class 'init (lambda (this) (stack/set-num-items! this 0) (stack/set-item-vector! this (make-vector 10)))) (class/set-method! stack-class 'top (lambda (this) (vector-ref (stack/get-item-vector this) (- (stack/get-num-items this) 1)))) (class/set-method! stack-class 'push (lambda (this item) (vector-set! (stack/get-item-vector this) (stack/get-num-items this) item) (stack/set-num-items! this (+ (stack/get-num-items this) 1)))) (class/set-method! stack-class 'pop (lambda (this) (if (stack/empty? this) (error "can't pop an empty stack") (let ((item (stack/top this))) (stack/set-num-items! this (- (stack/get-num-items this) 1)) item)))) (class/set-method! stack-class 'height (lambda (this) (stack/get-num-items this))) (class/set-method! stack-class 'empty? (lambda (this) (zero? (stack/height this)))) (class/set-method! stack-class 'display (lambda (this) (do ((n (- (stack/get-num-items this) 1) (- n 1))) ((negative? n)) (display " ") (display (vector-ref (stack/get-item-vector this) n)))))