Solution4 Lisp

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

;;;

;;; solution4.lisp: Solution for selected exercises in LISP tutorial 4


;;; Philip W. L. Fong
;;; SFU CMPT 310 (2001-1)
;;;

;;
;; Global stack abstraction
;;

(defparameter *stack* nil


"Global stack")

(defun global-stack-push (x)


"Push X into global stack."
(push x *stack*))

(defun global-stack-pop ()
"Pop the global stack."
(pop *stack*))

(defun global-stack-empty-p ()
"Check if global stack is empty."
(null *stack*))

;;
;; Encapsulated Stack object
;;

(defun make-stack ()
"Create a new stack object."
(let ((stack nil))
(list #'(lambda (x)
(push x stack))
#'(lambda ()
(pop stack))
#'(lambda ()
(null stack)))))

(defun stack-push (stack x)


"Push X into STACK."
(funcall (first stack) x))

(defun stack-pop (stack)


"Pop stack."
(funcall (second stack)))

(defun stack-empty-p (stack)


"Check if stack is empty."
(funcall (third stack)))

You might also like