Parsing/RPN calculator algorithm: Difference between revisions

adding lambdatalk version
m (→‎{{header|Phix}}: syntax coloured, also slightly less precision on penultimate output line due to recent printf() mods)
(adding lambdatalk version)
Line 2,781:
The final value is 3.0001220703125
</pre>
 
==[[header|Lambdatalk]]==
<lang Scheme>
{calc 3 4 2 * 1 5 - 2 3 pow pow / +}
-> 3.0001220703125
 
where
 
{def calc
{def calc.r
{lambda {:x :s}
{if {empty? :x}
then {car :s}
else {calc.r {cdr :x}
{if {unop? {car :x}}
then {cons {{car :x} {car :s}} {cdr :s}}
else {if {binop? {car :x}}
then {cons {{car :x} {car {cdr :s}} {car :s}} {cdr {cdr :s}}}
else {cons {car :x} :s}} }}}}}
{lambda {:s}
{calc.r {list :s} nil}}}
 
using the unop? & binop? functions to test unary and binary operators
 
{def unop?
{lambda {:op}
{or {W.equal? :op sqrt} // n sqrt sqrt(n)
{W.equal? :op exp} // n exp exp(n)
{W.equal? :op log} // n log log(n)
{W.equal? :op cos} // n cos cos(n)
... and so // ...
}}}
 
{def binop?
{lambda {:op}
{or {W.equal? :op +} // m n + m+n
{W.equal? :op -} // m n - m-n
{W.equal? :op *} // m n * m*n
{W.equal? :op /} // m n / m/n
{W.equal? :op %} // m n % m%n
{W.equal? :op pow} // m n pow m^n
... and so on // ...
}}}
 
and using the list and empty? functions to create
a list from a string and test its emptynes.
 
{def list
{lambda {:s}
{if {W.empty? {S.rest :s}}
then {cons {S.first :s} nil}
else {cons {S.first :s} {list {S.rest :s}}}}}}
 
{def empty?
{lambda {:x}
{W.equal? :x nil}}}
 
Note that everything is exclusively built on 5 lambdatalk primitives:
- "cons, car, cdr", to create lists,
- "W.equal?" which test the equality between two words,
- and the "or" boolean function.
</lang>
 
=={{header|Liberty BASIC}}==