Âè10²ó Scheme¤ÎÁí¹çÎý½¬¤Î²òÅúÎã

¢£ Îý½¬1

(define counter
  (lambda (x)
    (define n x)
    (define up
      (lambda (d)
        (set! n (+ n d))
        n))
    (define reset
      (lambda ()
        (set! n x)
        n))
    (define receive-message
      (lambda (msg)
        (cond ((equal? msg 'up) up)
              ((equal? msg 'reset) reset))))
    receive-message))

¢£ Îý½¬2

(define bag
  (lambda ()
    (define mybag ())
    (define error
      (lambda ()
        (write 'error)))
    (define show
      (lambda ()
        mybag))
    (define add
      (lambda (obj)
        (set! mybag (cons obj mybag))))
    (define get-one
      (lambda ()
        (if (null? mybag)
            (error)
            ((lambda (x) (set! mybag (cdr x)) (car x)) mybag))))
    (define receive-message
      (lambda (msg)
        (cond ((equal? msg 'show) show)
              ((equal? msg 'add) add)
              ((equal? msg 'get-one) get-one)
              (else error))))
    receive-message))

(c) 2009.7.16 by tokuhisa