Last active
December 16, 2015 09:48
-
-
Save Denommus/5415395 to your computer and use it in GitHub Desktop.
A calculator using reverse polish notation, written in Common Lisp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/sbcl --script | |
| (loop for n = (read t) | |
| for stack = (list n) then (cons n stack) | |
| until (member n '(exit quit)) | |
| do (unless (numberp n) | |
| (cond | |
| ((endp (nthcdr 2 stack)) | |
| (format t "Not enough numbers in stack~%") | |
| (pop stack)) | |
| (t | |
| (let ((fun (pop stack)) | |
| (op2 (pop stack)) | |
| (op1 (pop stack))) | |
| (push (funcall fun op1 op2) stack))))) | |
| (format t "(~{~A~^ ~})~%" (reverse stack))) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using "do" instead of "loop":