Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Lisp: How to get the reference (not the value) of an element in a 2D Matrix represented as a List of Lists?

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))"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This seems strange, as I thought that the nth method can be used to get references within a list?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading