title: Operator precedence prefix conversion - Scheme
body: <p>I am writing a function that takes a quoted arithmetic infix expression involving numbers, variables, and operators and converts it to prefix notation. </p>
  
  <p>For example:</p>
  
  <pre><code> (infix-&gt;prefix '(2 + 3 * x ^ 5 + a))
  </code></pre>
  
  <p>would evaluate to</p>
  
  <pre><code>(+ 2 (+ (* 3 (^ x 5)) a))
  </code></pre>
  
  <p>or </p>
  
  <pre><code>(+ (+ 2 (* 3 (^ x 5))) a)
  </code></pre>
  
  <p>In order of precedence we have: +,-,*,/, and  ^. </p>
  
  <p>This is what I have so far</p>
  
  <pre><code> (define (infix-&gt;prefix lst) 
     (if (list? lst)
     (if (null? (cdr lst))
        (car lst)
        (list (cadr lst)
              (infix-&gt;prefix (car lst))
              (infix-&gt;prefix (cddr lst)))
        )
     lst)
    )
  </code></pre>
  
  <p>This gives the correct prefix notation but with no precedence. It evaluates to</p>
  
  <pre><code>(+ 2 (* 3 (^ x (+ 5 a))))
  </code></pre>
  
  <p>Which is the right order but the parenthesis are off because of the precedence. I have done some research and am having a hard time figuring out how to add it. </p>
  
  <p>Any feedback or suggestions on how to refactor my code would be awesome. Thanks!</p>