A Lisp program for calculating postfix operation

An interesting lisp program. It calculates postfix operations. See if you have better solutions.

(defun pfeval (l)
; takes a list containing a postfix expression
; returns the value of that expression.
; finally test (pfeval '(16 2 3 ^ / 4 5 * + 16 3 * -))  
  (cond ((and (numberp (car l)) (null (cdr l)))  l)
        ((act-calculate nil l))))
(defun act-calculate (st exp)
; takes a stack and a postfix expression
; return the actual evaluation 
  (cond ((null exp) st)    
        ((numberp (car exp)) (act-calculate (cons 
                                             (car exp) 
                                             st ) 
                                            (cdr exp)))
        ((equal (car exp) '^)  (act-calculate (cons 
                                               (expt  (car (cdr st)) (car st) )
                                               (cddr st))
                                              (cdr exp)))
        (t  (act-calculate (cons 
                            (eval (cons (car exp) (cons (car (cdr st)) (cons (car st) nil))))
                            (cddr st))
                           (cdr exp)) )))
Category >> CS Courses  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";

Leave a comment