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

K&R C is the best book ever

Name: Anonymous 2010-01-01 21:49

Holy shit, it's teaching me how to make a Reverse Polish Notation calculator!

Name: Anonymous 2010-01-01 22:19

Holy shit, I saw OP's task and the thought of how easy it would be to implement it made me write the following:

(deftype operation () '(member + - * /))
(defun rpn-eval (exp &aux (stack nil)) 
  (dolist (e exp (first stack))
    (push
     (typecase e
       (integer e)
       (operation
        (let ((v1 #1=(pop stack)) (v2 #1#)) (funcall e v2 v1))))
     stack)))
; (rpn-eval '(5 1 2 + 4 * + 3 -)) => 14

It doesn't check if the stack is empty, but that would only be like 2 extra lines of code for an added assert.

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