One of the exercises in Advanced R section 2.5 on modify-in-place asks why the following code doesn’t create a circular list:
x <- list()
x[[1]] <- x
Advanced R Solutions says that it isn’t circular due to triggering copy-on-modify. However, at the beginning of the section Wickham said that “If an object has a single name bound to it, R will modify it in place.” Why does modify-in-place not then apply here? My thinking was that perhaps when you set x[[1]] <- x
you are binding a second name to the object, but it seems like that would also simultaneously unbind x
from the empty list because it redefines x
to be the list containing the empty list as its first element. Is it that R somehow doesn’t register the unbinding of x
until after the binding to x[[1]]
, or is there something else I’m missing here?