[plt-scheme] Re: Lexical Scoping, References and Side Effects

From: Erich Rast (erich at snafu.de)
Date: Thu Sep 3 08:09:19 EDT 2009

> Like I said, I'm
> just getting into Scheme and I'm pretty green.

Take a look at this version:

(define make-stack
  (lambda ()
    (let ((li null))
      (lambda (selector . rest)
        (case selector
          ((push!) (set! li (append rest li)))
          ((pop!) (begin0 (car li) (set! li (cdr li))))
          ((empty?) (empty? li))
          (else (error 'stack "unknown function '~s'" selector)))))))

(define a (make-stack))
(a 'push! 10 20 30)
(a 'pop!)
==> 10
(a 'pop!)
==> 20

You probably shouldn't use these kind of self-made object systems in
real life but it's good if you understand how the above example works.

Best,

Erich



Posted on the users mailing list.