title: Functions to print and replace elements in a list
body: <p>I am trying to implement two functions : subterm and replace.</p>
  
  <ol>
  <li>subterm takes two lists as arguments and prints the element in the first list that is reached after exhausting the second list.</li>
  </ol>
  
  <p>For example, calling </p>
  
  <p><code>(subterm '(1 2 (3 4 5) (6 (7 (8) 9 10))) '(4 2 2 1))</code> </p>
  
  <p>should return</p>
  
  <pre><code>8
  </code></pre>
  
  <p>I have come up with the following function which prints the nth element in the list :</p>
  
  <pre><code>(define (subterm list n)                   
    (cond 
      ((null? list) '())             
      ((= n 1) (car list))              
      (else (subterm (cdr list) (- n 1)))))
  </code></pre>
  
  <ol start="2">
  <li>replace takes 3 lists and returns the result of replacing the reached value with the rest of the list unchanged.</li>
  </ol>
  
  <p>for example calling :</p>
  
  <pre><code> (replace '(1 2 (3 4 5) (6 (7 (8) 9 10))) '(11 12) '(4 2 2 1))
  </code></pre>
  
  <p>should return :</p>
  
  <pre><code>'(1 2 (3 4 5) (6 (7 ((11 12)) 9 10)))
  </code></pre>
  
  <p>Again, I came up with this code which replaces the nth element in the first list with the second list, leaving the rest of the first list unchanged :</p>
  
  <pre><code>#lang racket
  (define (replace list elem n)
    (cond
      ((empty? list) empty)
      ((eq? n 1) (cons elem (cdr list)))
      (#t (cons (car list) (replace (cdr list) elem (- n 1))))))
  </code></pre>
  
  <p>How do I modify these functions to take in two lists?</p>
  
  <p>Edit 1:
  Some examples:</p>
  
  <pre><code>&gt; (subterm '(1 2 3 4 5) '(3))
  3
  
  &gt; (subterm '(1 2 3 4 5) '(2))
  2
  
  &gt; (subterm '(1 2 (3 4 5) 6 7) '(3 2))
  4
  </code></pre>
  
  <p>Consider this example:</p>
  
  <pre><code>&gt; (subterm '(1 2 (3 4 5) (6 (7 (8) 9 10))) '(4 2 2 1))
  8
  </code></pre>
  
  <p>In the above example, subterm takes 2 lists. Then it reads the second list. The second list basically tells subterm to return the 1st element (8) of the 2nd element ((8)) of the 2nd element (7 (8) 9 10) of the 4th element (6 (7 (8) 9 10) of the first list (1 2 (3 4 5) (6 (7 (8) 9 10))).</p>
  
  <pre><code>&gt; (subterm '1 '())
  1
  
  &gt; (subterm '(1 2 (3 4 5) (6 (7 (8) 9 10))) '())
  '(1 2 (3 4 5) (6 (7 (8) 9 10)))
  
  &gt; (replace '(1 2 3 4 5) '(6 7 8) '(3))
  '(1 2 (6 7 8) 4 5)
  
  &gt; (replace '(1 2 3 4 5) '(6 7 8) '(2))
  '(1 (6 7 8) 3 4 5)
  </code></pre>
  
  <p>Consider this example:</p>
  
  <pre><code>&gt; (replace '(1 2 (3 4 5) 6 7) '(8 9) '(3 2))
  '(1 2 (3 (8 9) 5) 6 7)
  </code></pre>
  
  <p>replace takes in three lists: first list is the list in which elements have to be replaced. The second list contains the new elements which have to be put into the first list. The third list contains the positions where the elements have to be replaced.
  So, it basically replaced the 2nd element (4) of the 3rd element (3 4 5) of the first list (1 2 (3 4 5) 6 7).</p>
  
  <pre><code>&gt; (replace '(1 2 (3 4 5) (6 (7 (8) 9 10))) '(11 12) '(4 2 2 1))
  '(1 2 (3 4 5) (6 (7 ((11 12)) 9 10)))
  
  &gt; (replace '(1 2 (3 4 5) (6 (7 (8) 9 10))) 1000 '(4 2 2 1))
  '(1 2 (3 4 5) (6 (7 (1000) 9 10)))
  
  &gt; (replace '(1 2 (3 4 5) (6 (7 (8) 9 10))) 'x '())
  'x
  
  &gt; (replace '1 '(2 3 4) '())
  '(2 3 4)
  </code></pre>