;;; Procedure: ;;; make-stack ;;; Parameters: ;;; None. ;;; Purpose: ;;; Create a new, empty stack. ;;; Produces: ;;; stack, a procedure. ;;; Preconditions: ;;; [None] ;;; Postconditions: ;;; stack responds to the following messages ;;; :push! value ;;; Adds value to the top of the stack. ;;; :pop! ;;; Returns and removes the value still in the stack that was most ;;; recently added by push!. ;;; :peek ;;; Returns and does not remove the value still in stack that was ;;; most recently added by push!. ;;; :empty? ;;; Determines if the stack is empty. (define make-stack (lambda () (let ((vec (make-vector 1 null))) (lambda (message . things) (cond ((eq? message ':empty?) ; See if the list in the vector is null (null? (vector-ref vec 0))) ((eq? message ':peek) (if (null? (vector-ref vec 0)) (error "Can't peek at an empty stack.") (car (vector-ref vec 0)))) ((eq? message ':push!) (vector-set! vec 0 (cons (car things) (vector-ref vec 0)))) ((eq? message ':pop!) (let ((result (car (vector-ref vec 0)))) (vector-set! vec 0 (cdr (vector-ref vec 0))) result)) (else (error "Can't understand that meaningless message.")))))))