title: Scheme / Racket insert-everywhere function with sublists
body: <p>So I've been trying to solve this problem:</p>
  
  <p>Given an element E and a list L insert E into every position in the list L (so the result is a list of lists). Example:
  <code>(insert-everywhere 'a '(b c))</code> would give <code>((a b c) (b a c) (b c a))</code></p>
  
  <p>This is easy enough but there is one other condition in my problem that is making it difficult for me - if an element of L is a list itself then the element must also be inserted into every position in the sublist. For example:</p>
  
  <p><code>(insert-everywhere 'd '(a (b c)))</code> would return: <code>((d a (b c)) (a d (b c)) (a (d b c)) (a (b d c)) (a (b c d)) (a (b c) d))</code>.</p>
  
  <p>This is the code I have so far (which I mostly lifted from <a href="https://codereview.stackexchange.com/questions/11805/insert-everywhere-function">here</a>):</p>
  
  <pre><code>#lang racket
  (define (insert-at pos elmt lst) 
   (if (empty? lst) (list elmt)
   (if (list? (car lst)) (insert-everywhere elmt (car lst))
   (if (= 1 pos)
    (cons elmt lst)
    (cons (first lst) 
          (insert-at (- pos 1) elmt (rest lst)))))))
  
  (define (insert-everywhere sym lst)
    (remove-duplicates
      (map (lambda (i)
        (insert-at i sym lst))
          (range 1 (+ 2 (length lst))))))
  </code></pre>
  
  <p>where this line: <code>(if (list? (car lst)) (insert-everywhere elmt (car lst))</code> is supposed to handle the sublists but it isn't working. (If I run <code>(insert-everywhere 'd '(a (b c)))</code> with the above code I get <code>((d a (b c)) (a (d b c) (b d c) (b c d)))</code>)</p>
  
  <p>If anyone has suggestions on how to approach this differently I'd be happy to hear.</p>