The folks over on the SNOBOL4 mailing list have been having
fun with a recent posting pointing to a web page collecting
"reverse-polish-notation calculators" written in different
languages. The web site is http://www.stacken.kth.se/~foo/rpn/.
Anyway, I thought folks might like to see a couple of Unicon
solutions (the first is an improvement of one that I've
submitted to the web page, the second is a more general
solution). The code is 'clever', meaning I wouldn't write
it this way for money.....ok, I *would* write it this way
for enough money.
Solution 1: Handles binary operators only:
-----------------------------------------------------------------
invocable all
procedure main(stk,ws:' \t')
while write("-> ",read() ? (while (pos(0) & break stk) |
push(stk, numeric(x := (tab(upto(~ws)),tab(many(~ws)))\1) |
proc(x,2)!(a:=pop(stk),[pop(stk),a])) |
break ["error"] )[1])
end
------------------------------------------------------------------
Solution 2: (handles unary operations also) [note that RPN has
an inherent ambiguity with binary and unary operations using
the same symbol. This solution always resolves that ambiguity
in favor of the binary operation]:
------------------------------------------------------------------
invocable all
procedure main(stk,ws:' \t')
while write("-> ",read() ? (while (pos(0) & break stk) |
push(stk,numeric(x := (tab(upto(~ws)),tab(many(~ws)))\1) |
(p := proc(x,3|2|1))!getArgs(stk,args(p),[]) ) |
break ["error"] )[1])
end
procedure getArgs(stk, i, a) # need args in reverse of stack order
every j := 1 to i do push(a, pop(stk)|fail)
return a
end
----------------------------------------------------------------
Some sample runs of the 2nd version (note that the stack is
preserved from one input line to the next, allowing expressions
to span multiple lines). I've insert blank lines for clarity
and split the one long output line into multiple lines so it
fits:
----------------------------------------------------------------
weaver% arpn2
1 2 sin
-> 0.9092974268256817
+
-> 1.909297426825682
1 2 real /
-> 0.5
5 -
-> -4.5
1 1 1 1.0 + + /
-> 0.3333333333333333
2 1000 ^
-> 10715086071862673209484250490600018105614048117055336074437503
88370351051124936122493198378815695858127594672917553146825187145
28569231404359845775746985748039345677748242309854210746050623711
41877954182153046474983581941267398767559165543946077062914571196
477686542167660429831652624386837205668069376
stop
weaver% arpn2 1 1 1 1.0
+ + /
-> 0.3333333333333333
stop
weaver%
-----------------------------------------------------------------
--
Steve Wampler -- [EMAIL PROTECTED]
The gods that smiled on your birth are now laughing out loud.
-------------------------------------------------------
This SF.Net email is sponsored by: YOU BE THE JUDGE. Be one of 170
Project Admins to receive an Apple iPod Mini FREE for your judgement on
who ports your project to Linux PPC the best. Sponsored by IBM.
Deadline: Sept. 24. Go here: http://sf.net/ppc_contest.php
_______________________________________________
Unicon-group mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/unicon-group