3

我想通过类的插槽名称列表重复出现,两个类的插槽名称相同((current-trial *exp*)(previous *exp*)指的是同一类的实例)。在每次递归时,我想评估插槽名称,以便可以获取和设置该实例的该插槽的值。下面的代码满足了这个期望,但我担心依赖 eval 因为它很慢并且不允许词法上下文(Graham,1996)。什么是有效的并允许词汇上下文的替代表述?

(dolist (a '(letter number font color height))
  (eval 
   `(when (eq (,a (current-trial *exp*)) 
              (,a (previous *exp*)))
     (setf (,a (current-trial *exp*)) 
           (random-not-item 
            (,a (current-trial *exp*)) 
            (,a *exp*))))))
4

1 回答 1

5

由于这些是插槽名称,您可以使用slot-value

(dolist (a '(letter number font color height))
  (when (eq (slot-value (current-trial *exp*) a) 
            (slot-value (previous *exp*) a))
    (setf (slot-value (current-trial *exp*) a) 
          (random-not-item 
           (slot-value (current-trial *exp*) a) 
           (slot-value *exp* a)))))
于 2013-08-23T18:24:36.623 回答