[plt-scheme] Re: Lexical Scoping, References and Side Effects
> 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