Here is my code about postfix in scheme:
(define (stackupdate e s)
(if (number? e)
(cons e s)
(cons (eval '(e (car s) (cadr s))) (cddr s))))
(define (postfixhelper lst s)
(if (null? lst)
(car s)
(postfixhelper (cdr lst) (stackupdate (car lst) s))))
(define (postfix list)
(postfixhelper list '()))
(postfix '(1 2 +))
But when I tried to run it, the compiler said it takes wrong. I tried to check it, but still can't find why it is wrong. Does anyone can help me? Thanks so much! And this is what the compiler said:
e: unbound identifier;
also, no #%app syntax transformer is bound in: e
evalnever has any information about variables that some how are defined in the same scope as it is used. Thuseandsdoes not exist. Usuallyevalis the wrong solution, but if you are to use eval try doing it as as little as you can:Now instead of
(eval '(e (car s) (cadr s)))you do((symbol->proc e) (car s) (cadr s)). Now you should try(postfix '(1 2 pair?))I've made many interpreters and none of them used
eval. Here is what I would have done most of the time:This fixes the
(postfix '(1 2 pair?))problem. A thing that I see in your code is that you always assume two arguments. But how would you do adouble? eg something that just doubles the one argument. In this casesymbol->proccould return more information:And in your code you could do this if it's not a number:
takeanddropare not R5RS, but they are simple to create.