スタックオブジェクトを作ってみる
(define (create-stack) (let ((*s* '())) (lambda (op . args) (cond ((eq? op 'push) (set! *s* (cons (car args) *s*))) ((eq? op 'pop) (if (null? *s*) '() ((lambda (ret) (set! *s* (cdr *s*)) ret) (car *s*)))) (#t '())))))
使い方は次のようになります。
(define p (create-stack)) ;; => p (define q (create-stack)) ;; => q (p 'push 1) ;; => (1) (p 'push 2) ;; => (2 1) (q 'push 'a) ;; => (a) (q 'push 'b) ;; => (b a) (p 'pop) ;; => 2 (p 'pop) ;; => 1 (p 'pop) ;; => () (q 'pop) ;; => b (q 'pop) ;; => a (q 'pop) ;; => ()
若干括弧に対するアレルギーが薄れてきました!