7.9

## Reverse Polish Notation

 (require rpn) package: rpn

Reverse Polish Notation (RPN) is a way of writing expressions commonly found in calculators and Forth-like programming languages. In RPN, an expression is a list of instructions for manipulating data on a stack. The most basic instruction is to push a value onto the stack. Function call instructions pop a function’s arguments off the stack and push its results onto the stack. The rpn library represents an RPN program as a call to the rpn function with the program instructions as arguments.

Examples:
 > (rpn 1) (rpn-stack 1) > (rpn 1 2 3) (rpn-stack 1 2 3) > (rpn 1 2 3 r*) (rpn-stack 1 6) > (rpn 1 2 3 r* r+) (rpn-stack 7) > (rpn 1 2 3 r* r+ 2) (rpn-stack 7 2) > (rpn 1 2 3 r* r+ 2 r-) (rpn-stack 5)

### 1RPN Stacks

 procedure v : any/c
 value
 procedure(rpn-stack-push stack instruction) → rpn-stack? stack : rpn-stack? instruction : rpn-instruction?

### 2RPN Instructions

 procedure v : any/c

#### 2.1Pushing Operands

 procedure v : any/c
 procedure v : any/c
 procedure(rpn-operand-value operand) → any/c operand : rpn-operand?

#### 2.2Calling Operators

 procedure v : any/c
 procedure(binary-rpn-operator binary-function) → rpn-operator? binary-function : (-> any/c any/c any/c)

procedure

 (rpn-operator #:function function #:input-arity input-arity #:output-arity output-arity) → rpn-operator?
function : procedure?
input-arity : natural?
output-arity : natural?

 procedure(rpn-operator-function operator) → procedure? operator : rpn-operator?
 procedure(rpn-operator-input-arity operator) → natural? operator : rpn-operator?
 procedure(rpn-operator-output-arity operator) → natural? operator : rpn-operator?

 value
 value
 value
 value

### 3RPN Notation

 procedure(rpn instruction ...) → rpn-stack? instruction : any/c

 value