Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

scheme help pls

Name: Anonymous 2009-05-23 12:23

hey /prog/, I'm trying to do something simple and failing hard. I seem to be missing something obvious, but I'm not sure what. Using DrScheme here.

Suppose I have a list that represents infix notation, e.g.,
(1 + 2)
and I want to evaluate it. I can create a list that is in prefix notation assuming a simple list in this form:
(define infix->prefix (lambda (x)
                          (list (cadr x)
                                         (car x)
                                         (caddr x))))

And then
> (infix->prefix '(2 + 3))
(list '+ 2 3)
>

But, er, how would I evaluate this? That is, what am I missing so that
> (infix->prefix '(2 + 3))
5
>


Thanks for any insight.

Name: Anonymous 2009-05-28 10:19

Fuck Scheme and fuck precedence too.

(defun i->p (x)
   (cond ((atom x) x)
         ((cdr  x) (let ((y (reverse x)))
                      (list (cadr y) (i->p (reverse (cddr y))) (i->p (car y)))))
         (t        (car x))))

(defmacro infix x (i->p x))

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List