title: scheme replace a1 in a list of lists with a2, both a1 a2 is flat
body: <p>Going further for the <a href="https://stackoverflow.com/questions/26747178/scheme-how-do-you-sum-numbers-in-a-list-when-you-have-structures-and-list-of-lis/26755096#26755096">scheme how do you sum numbers in a list when you have structures and list of lists</a> now i want to replace all occurrences of atom a1 in SEXP with atom a2. </p>
  
  <p>For example,</p>
  
  <pre><code>(replace (list (list 'a 'b) (list 1 3 )) 'a 'b) =&gt;
  (list (list 'b 'b) (list 1 3))
  
  (replace (list (list 'a 'b) (list 1 3)) 1 2) =&gt;
  (list (list 'a 'b) (list 2 3))
  
  ;; An ATOM is one of: 
  ;; -- Symbol
  ;; -- String 
  ;; -- Number
  
  (define-struct SEXP (ATOM SEXP))
  
  ;; An SEXP (S-expression) is one of: 
  ;; -- empty 
  ;; -- (cons ATOM SEXP)
  ;; -- (cons SEXP SEXP)
  </code></pre>
  
  <p>My code, </p>
  
  <pre><code>;; replace: Atom atom sexp -&gt; sexp
  
  (define (replace a1 a2 sexp)
     (cond
        [(empty? sexp) empty]
        [(SEXP? sexp)
               (cons (replace a1 a2 (first sexp)) (replace a1 a2 (rest sexp)))]
        [else
           (cond 
               [(or (symbol=?  (first sexp) a1)
                    (string=?  (first sexp) a1)
                    (=  (first sexp)  a1))
                (cons a2 (replace a1 a2 (rest sexp)))]
               [else (cons (first sexp) (replace a1 a2 (rest sexp)))])])) 
  </code></pre>
  
  <p>for the part to determine if it is a1, do we need call a helper function we could do so in my way present here? </p>
  
  <p>Also, since sexp is a list of lists and atom is flat, </p>
  
  <pre><code> (symbol=?  (first sexp) a1) 
  </code></pre>
  
  <p>might cause problem since scheme would expect a symbol but given (list xxxxxx) </p>
  
  <p>How do you fix that problem as well? </p>