小编net*_*ave的帖子

普通口齿不清的地方政府

Common Lisp中的新手问题:

如何使我的过程每次调用时都返回具有自己的本地绑定的不同过程对象?当前,我使用let创建本地状态,但是两个函数调用共享相同的本地状态。这是代码,

(defun make-acc ()
  (let ((balance 100))
    (defun withdraw (amount)
      (setf balance (- balance amount))
      (print balance))
    (defun deposit (amount)
      (setf balance (+ balance amount))
      (print balance))
    (lambda (m)
      (cond ((equal m 'withdraw)
              (lambda (x) (withdraw x)))
            ((equal m 'deposit)
              (lambda (x) (deposit x)))))))

;; test

(setf peter-acc (make-acc))

(setf paul-acc (make-acc))

(funcall (funcall peter-acc 'withdraw) 10)
;; Give 90

(funcall (funcall paul-acc 'withdraw) 10)
;; Expect 90 but give 80
Run Code Online (Sandbox Code Playgroud)

我应该用其他方式吗?我的写作方式有误吗?有人可以帮我解决这个疑问吗?提前致谢。

lisp state common-lisp let

8
推荐指数
3
解决办法
86
查看次数

标签 统计

common-lisp ×1

let ×1

lisp ×1

state ×1