Replace elements in nested quoted lists adds new elements?

前端 未结 3 907
遇见更好的自我
遇见更好的自我 2020-12-22 09:48

I have a nested list, and I am trying to non-destructively replace all its elements (inside the nested list as well). That is, given my input list

\'(1 \'(2         


        
3条回答
  •  生来不讨喜
    2020-12-22 10:37

    You have to realize that 'foo is syntactic sugar for (quote foo), so when you use quotes inside an already-quoted list, this:

    '(1 '(2 3 4) '(5 6 7) 8 9)
    

    evaluates to this:

    (1 (quote (2 3 4)) (quote (5 6 7)) 8 9)
    

    So when you substitute all the list elements with 0, you get:

    (0 (0     (0 0 0)) (0     (0 0 0)) 0 0)
    

    You either need to not put extra quotes in your examples, or you need to handle the quote operator specially in subs-list:

    (defun subs-list (list value)
      "Replaces all elements of a list of list with given value"
    
      (loop for elt in list
           collect
             (cond 
               ((listp elt) 
                 (subs-list elt value))
               ((eq 'quote elt)
                 elt)
               (t
                 value))))
    

提交回复
热议问题