title: LISP: Why doesn't mapcan accept my list give as parameters?
body: <p>To simplify my question:
  why this works</p>
  
  <pre><code>(mapcan #'(lambda (l) (list '1 '2) ) '(a b))
  </code></pre>
  
  <p>and this doesn't </p>
  
  <pre><code>(mapcan #'(lambda (l) '(1 2) ) '(a b))
  </code></pre>
  
  <p>?</p>
  
  <p>I have to write a function that substitutes an element through all elements of list D at all levels of a given list L, using Map Functions.
  I tried using <code>mapcan</code> for this:</p>
  
  <pre><code>(defun subAll (l k d)
    (cond 
      ((and (atom l) (eq l k)) 
       d)
      ((and (atom l) (not (eq l k)))
       (cons l '()))
      (t (cons 
           (mapcan #'(lambda (l) (subAll l k d)) l)
           '()))))
  </code></pre>
  
  <p>but I get following results for these 2 inputs:</p>
  
  <p>1.</p>
  
  <pre><code>(subAll '(1(2(3(4(5(6(7)))))) (2(3(4 7(4(4(4(4)))))))) '7 '(a b))
   =&gt; ((1(2(3(4(5(6(A B(4(4(4(4)))))))))) (2(3(4 A B(4(4(4(4)))))))))
  </code></pre>
  
  <p>2.</p>
  
  <pre><code>(subAll '(1 2 3 (4 2 (3 2 (2)))) '2 '(99 98))
  =&gt;Lisp stack overflow.
  </code></pre>
  
  <p>But if replace 
  <code>((and (atom l) (eq l k))  d)</code> with <code>((and (atom l) (eq l k))  (list 'a 'b))</code> it works for input 1. Also I`ve made my own function that just deconstructs a list and reconstructs it:</p>
  
  <pre><code>(defun lst(l)
  (cond
      ((null l) nil)
      (t (cons (car l) ( lst (cdr l))))))
  </code></pre>
  
  <p>and it work if a replace <code>((and (atom l) (eq l k))  d)</code> with <code>((and (atom l) (eq l k))  (lst d))</code> for both of my inputs above.</p>
  
  <ol>
  <li><p>=> <code>((1(2(3(4(5(6(A B)))))) (2(3(4 A B(4(4(4(4)))))))))</code></p></li>
  <li><p>=> <code>((1 A B 3 (4 A B (3 A B (A B)))))</code></p></li>
  </ol>
  
  <p>Does mapcan accept only a special kind of list? If anyone can explain to me why it does that or give another solution I would be grateful. ( I can't use any built in functions like list or append, only if I make my own append and list)</p>
  
  <p>I am using GNU CLISP 2.49</p>