lundi 13 février 2017

Scheme Infix to Prefix

Vote count: 0

I am trying to convert a Scheme expression from infix to prefix notation and output the result of the operation. The algorithm below will work if I input a list of 3. So (1 + 2) will result in 3 and (1 + (2 + 3)) will result in 6. However it does not work for a list of more than 3 elements. (1 + 2 + 3) results in 3. How would I go about resolving this issue?

(define (infix->prefix lst)
(cond
((list? lst) 

 (let ((operand1 (car lst))
       (operator (cadr lst))
       (operand2 (caddr lst)))
   (eval(list operator
         (infix->prefix operand1)
         (infix->prefix operand2)
         )
        ;close list
        )))
(else lst))
 )

asked 55 secs ago

Let's block ads! (Why?)



Scheme Infix to Prefix

Aucun commentaire:

Enregistrer un commentaire