0

I am trying to create a list variable that can be set to one list or another depending on a condition. What I have so far is as follows.

(let ((l (if local list1 list2)))
  ; Append items to the list
  )
  • local is a parameter that can be set to t or nil.
  • list1 and list2 are list variables.

Then when I later try to access either list1 or list2, I am getting the following error.

Lisp error: (wrong-type-argument sequencep t)

Essentially, what I am trying to do is update either list1 or list2 depending on the value of local. Perhaps this is what is causing my issue.

The following is the shortest code segment that illustrates the problem.

(defvar local-list '())
(defvar global-list '())

(defun add-item-to-list (local item)
  (let ((l (if local local-list global-list)))
    (add-to-list 'l item)
    )
  )

(defun print-local-list ()
  (mapc (lambda (item) (message item)) local-list)
  )

(add-item-to-list t "Blue")
(add-item-to-list t "Greem")
(add-item-to-list t "Red")
(print-local-list)
  • 4
    You need to add some information to replicate your issue. What function are you calling and what is the value of list1, local, list2, etc. try to create a code snippet that someone else can take and run. – Prgrm.celeritas Feb 14 at 19:24
  • 1
    Set debug-on-error to t and examine the stack. – sds Feb 14 at 19:36
  • Please show the code that uses l and expects a sequence. – Drew Feb 14 at 19:38
  • 2
    Copy-paste a complete code fragment that reproduces the issue. There is no problem in the code you show and we can't guess what you aren't showing. – Gilles Feb 14 at 20:41
  • After Ben Key has added a minimal example the problem is clear. The quoting in add-item-to-list is in the wrong order. local-list and global-list should be quoted not l. Therefore I vote to re-open the question. – Tobias Feb 15 at 8:56
3

You assign the value of local-list or global-list to the local variable l. Later you apply add-to-list to that local variable l. By default add-to-list prepends the new item to the list. That means a new first link is created and assigned to l. That does not change the list values of local-list nor global-list. (Note, if the value of l is nil it does not matter whether you prepend or append the new item.)

What you probably want is to assign one of the symbols local-list or global-list to l and use that selected symbol for add-to-list. In the following code the quote is shifted from add-to-list to the value of l. Thus l evaluates to one of the symbols local-list or global-list in the call of add-to-list and the new item is added to the list value of the resulting symbol (local-list or global-list).

Note also that (message item) is dangerous. The string item is interpreted as format string. If it contains a percent sign the character sequence after the percent sign is misinterpreted as format specifier. It is better to use #'princ or (lambda (item) (message "%s" item)).

(defvar local-list '())
(defvar global-list '())

(defun add-item-to-list (local item)
  (let ((l (if local 'local-list 'global-list)))
    (add-to-list l item)
    )
  )

(defun print-local-list ()
  (mapc #'princ local-list)
  )

(add-item-to-list t "Blue")
(add-item-to-list t "Greem")
(add-item-to-list t "Red")
(print-local-list)
  • Thanks. That is exactly what I needed to know. – Ben Key Feb 15 at 20:29

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.