[ create a new paste ] login | about

Project: programmingpraxis
Link: http://programmingpraxis.codepad.org/fjzlC50x    [ raw code | fork ]

programmingpraxis - Scheme, pasted on Feb 19:
1
2
3
4
5
6
7
8
9
10
11
12
13
#! /usr/bin/scheme script
(define (next)
  (cond ((eof-object? (peek-char)) (exit))
        ((char=? (peek-char) #\space) (read-char) (next))
        ((char=? (peek-char) #\newline) (read-char) nl)
        (else (read))))

(define (op token) (case token ((+) +) ((-) -) ((*) *) ((/) /)))

(let rpn ((token (next)) (stack ()))
  (cond ((eq? nl token) (display (car stack)) (newline) (rpn (next) (cdr stack)))
        ((number? token) (rpn (next) (cons token stack)))
        (else (rpn (next) (cons ((op token) (cadr stack) (car stack)) (cddr stack))))))


Create a new paste based on this one


Comments: