I’m writing a program in Common Lisp in which I need a function with this basic outline:
(defun example (initial-state modify mod-list)
(loop for modification in mod-list
collecting (funcall modify initial-state modification)))
The problem is that I need initial-state to be the same every time it is passed to modify, but modify can be destructive. I would simply make a copy, but I don’t want to make any assumptions about what type of data initial-state is.
How can I make this happen? Or is it even possible?
Thanks!
If the function can be destructive and you cannot do anything about it then it’s clear you need to make copies of
initial-state.One possibility to avoid preconfiguring what kind of data does
initial-statecontains is to leave providing a copy operation explicitly a problem for the caller or to make it a generic operation and relying on someone else to provide a method.The first version is more general because it allows the state to be any object while in the second version the copy operation depends on state object type (and this means you cannot have two callers both using lists as state with a different copy semantic).
However
copy-stateis a general operation that can be probably used in other places and making the operation a generic increase usability (you don’t need to pass around builder functions instead); it also allows the introduction of other generic operations likecompare-state,write-state,read-state…