Question

I want to recur through a list of slot names of classes, the same slot names for two classes ((current-trial *exp*) & (previous *exp*) refer to instances of the same class). On each recursion, I want to evaluate the slot name so that the value of that slot of that instance may be got and set. The code below meets that expectation, but I worry about relying upon eval because it's slow and doesn't allow for lexical context (Graham, 1996). What would be alternative formulations that would be efficient and allow for lexical context?

(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*))))))
Was it helpful?

Solution

Since these are slot names, you can use 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)))))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top