I want to define a struct for chores

(defstruct chore name sector assignments)

where the assignments slot is a vector with seven positions. I know that the above syntax will generate the getters chore-name, chore-sector and chore-assignments. But I would like to have something like this (chore-assignment 1 c). Is there any syntax that will give me this “for free” or should I code the function myself?


Yes and no. No, because chore-assignment doesn't let you modify what's inside the value of the assignment slot; it only lets you modify the assignment slot itself.

Instead, Lisp has setf, which has some really good tricks up its sleeves. The first argument to setf is a PLACE, which is any expression which can be interpreted as a place to store a value. Often this is just a variable name, but it can be much more complex. Just based on your question, I think you've already been using something like this:

(setf (chore-assignments c) (vector foo bar baz))

You can also do more:

(setf (aref (chore-assignments c) 1) 42)

Note that the PLACE in this assignment is not the whole assignments vector, it's one individual element inside the vector, exactly as you wanted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.