I am trying to reference a specific element in a 2D Matrix represented as list of Lists so I can set that specific element to 0. However, when I run the code below:
(defvar listOfLists '( (1 3) (2 6) ))
(defvar referenceToTopLeftCorner (nth 0 (nth 0 listOfLists)))
(setq referenceToTopLeftCorner 0)
(print (format nil "listsOfLists = ~a" listOfLists))
The following output is:
""listsOfLists = ((1 3) (2 6))"
This seems strange, as I thought that the nth
method can be used to get references within a list?
The variable you called reference is not a reference.
You need to do
(setf (nth 0 (nth 0 list-of-lists)) 0)
Note that you could also do
(defvar 1st-list (first list-of-lists))
(setf (first 1st-list) 0)
for the same effect, because 1st-list
refers to the 1st list in list-of-lists
.
You could also use define-symbol-macro
:
(define-symbol-macro reference-to-top-left-corner
(first (first list-of-lists)))
(setf reference-to-top-left-corner 0)
because now reference-to-top-left-corner
really is a reference.
I strongly advise you not to use symbol macros yet. Such advanced tools should be used sparingly and cautiously.