git @ Cat's Eye Technologies define-opaque / master eg / stack.scm
master

Tree @master (Download .tar.gz)

stack.scm @masterraw · history · blame

; The content of this file is in the public domain.
; example usage with Chicken Scheme: csi -q -b stack.scm

(load "define-opaque-0.2.scm")

(define-opaque stack make-stack (items) ('())
  (
    (new (lambda (new-items)
      (make-stack new-items)))
    (push (lambda (item)
      (make-stack (cons item items))))
    (top (lambda ()
      (car items)))
    (pop (lambda ()
      (make-stack (cdr items))))
  )
)

(define demo (lambda ()
  (let* (
    (stack0 (stack 'new '(4 5 6)))
    (stack1 (stack0 'pop))
    (stack2 (stack1 'push 9))
    (stack3 (stack2 'pop))
    (stack4 (stack3 'push 8))
  )
    (display (stack0 'top)) (newline)
    (display (stack1 'top)) (newline)
    (display (stack2 'top)) (newline)
    (display (stack3 'top)) (newline)
    (display (stack4 'top)) (newline)
  )))

(demo)