Inverted syntax: Difference between revisions
Content added Content deleted
m (→{{header|Go}}: Added note about inverted syntax with assignment.) |
(→{{header|Factor}}: Be smarter about it with macros) |
||
Line 328: | Line 328: | ||
[ map [ sq ] { 1 2 3 4 5 } ] reverse call ! { 1 4 9 16 25 }</lang> |
[ map [ sq ] { 1 2 3 4 5 } ] reverse call ! { 1 4 9 16 25 }</lang> |
||
In fact, using a Lisp-style macro, we can perform this transformation at parse time: |
|||
<lang factor>MACRO: pre ( quot -- quot ) reverse ; |
|||
[ + 2 2 ] pre ! 4</lang> |
|||
Of course, this isn't true prefix because <code>+</code> retains its arity of <tt>2</tt>: |
|||
<lang factor>[ + 3 + 2 2 ] pre ! 7</lang> |
|||
We can define a more accurate prefix macro for addition and subtraction in terms of <code>reduce</code>: |
|||
<lang factor>MACRO: pre ( quot -- quot ) 1 cut swap [ 0 ] dip reduce 1quotation ; |
|||
[ + 1 2 3 4 5 ] pre ! 15</lang> |
|||
Additionally, using parsing words, we can add any syntax we like. The <code>infix</code> vocabulary is an example of this: |
|||
<lang factor>USE: infix |
<lang factor>USE: infix |