Scanning from the RIGHT, an operand is pushed and an operator pops
exactly its arity; no parentheses and no precedence table are needed. A
malformed expression is an error, not a plausible wrong number.
Arguments
- tokens
Character vector of tokens in prefix order. Operators are
"+", "-", "*", "/", "^"; everything else must parse as a number.
Value
List with value, n_tokens, n_operators,
max_stack.
Details
Formula: scan right to left; operand -> push;
binary operator -> pop a, pop b, push (a op b)
References
Lukasiewicz, J. (1929/1963), Elements of Mathematical
Logic, in which the parenthesis-free notation is introduced; usually
dated to his 1924 lectures. The evaluation procedure is the standard
stack algorithm and is not attributed to that source.
Examples
Prefixev(tokens = 5L)
#> $value
#> [1] 5
#>
#> $n_tokens
#> [1] 1
#>
#> $n_operators
#> [1] 0
#>
#> $max_stack
#> [1] 1
#>
#> $method
#> [1] "Prefix (Polish) notation evaluation"
#>
#> attr(,"class")
#> [1] "morie_rich_result" "list"