title: Merging lists in recursive procedure in Scheme
body: <p>I am trying to perform the following action -
  i have a function that recieves a list and a number,
  for each occurrance of this number it should duplicate it in the output list,
  here is what i have so far:</p>
  
  <pre><code>(define (extendList lst num)
    (define data (car lst))
    (define nextData (cdr lst))
    (cond ((and (number? data)(= data num)) ( list num num (extendList nextData num)))
        (list? data) (list (extendList data num) (extendList nextData num))
        (else ( list data (extendList (cdr lst) num) ))))
  </code></pre>
  
  <p>The thing is that it just doesnt call to this function again in recursion - all it does is adding the next element to the output,
  for example here is my input and my expected output:</p>
  
  <pre><code>(extendList '(6 x y 6) 6) ; output should be: (6 6 x y 6 6)
  (extendList '(6 x (y 6) 6) 6) ;output should be: (6 6 x (y 6 6) 6 6)
  </code></pre>
  
  <p>but for both of the inputs it just returns the following:</p>
  
  <pre><code>'(6 6 x)
  </code></pre>
  
  <p>What am i doing wrong here?
  thanks in advance!</p>