Replace elements in nested quoted lists adds new elements?

醉酒当歌 提交于 2019-11-29 17:41:14
Joshua Taylor

Mark's answer and wdebeaum's answer explain why you're getting the results that you're getting; the nested quotes mean you've actually got a list like (1 (quote (2 3 4)) (quote (5 6 7)) 8 9), and you're replacing the symbol quote with 0, and that's why you get (0 (0 (0 0 0)) (0 (0 0 0)) 0 0). You probably want just '(1 (2 3 4) (5 6 7) 8 9) with no nested quotes.

It's worth pointing out that Common Lisp already provides a functions for non-destructively substituting in cons-trees, though: subst, subst-if, and subst-if-not. There are destructive versions, too: nsubst, nsubst-if, and nsubst-if-not. In particular, for this case you can just replace everything that's not a list with 0, either by using the complement function with listp and subst-if, or using listp and subst-if-not:

;; With `extra' elements because of the quotes:

(subst-if-not 0 #'listp '(1 '(2 3 4) '(5 6 7) 8 9))
;=> (0 (0 (0 0 0)) (0 (0 0 0)) 0 0) 

(subst-if 0 (complement #'listp) '(1 '(2 3 4) '(5 6 7) 8 9))
;=> (0 (0 (0 0 0)) (0 (0 0 0)) 0 0) 
;; With no `extra' elements:

(subst-if-not 0 #'listp '(1 (2 3 4) (5 6 7) 8 9))
;=> (0 (0 0 0) (0 0 0) 0 0)

(subst-if 0 (complement #'listp) '(1 (2 3 4) (5 6 7) 8 9))
;=> (0 (0 0 0) (0 0 0) 0 0)

If you wanted to take the hybrid approach suggested in wdebeaum's answer where you don't replace quotes, you can do that do:

(subst-if 0 (lambda (x)
              (not (or (listp x)
                       (eq 'quote x))))
          '(1 '(2 3 4) '(5 6 7) 8 9))
;=> (0 '(0 0 0) '(0 0 0) 0 0)

(subst-if-not 0 (lambda (x)
                  (or (listp x)
                      (eq 'quote x)))
          '(1 '(2 3 4) '(5 6 7) 8 9))
;=> (0 '(0 0 0) '(0 0 0) 0 0)

What am I doing wrong?

You have actually done a good work with loop and it works! Remember that ' stands for quote, so:

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

is equal to

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

you see, your quotes have been substituted too (except for the first one, which has been consumed during evaluation of the function argument)! One quote is enough to suspend execution.

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))))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!