Replicate: Difference between revisions

Jump to navigation Jump to search
2,179 bytes added ,  11:07, 18 March 2020
Operator vs. function test
(Operator vs. function test)
Line 74: Line 74:
c  ccc
c  ccc
</source>{{Works in|[[APL2]], [[Dyalog APL]], [[APLX]], [[ngn/APL]], [[GNU APL]]}}
</source>{{Works in|[[APL2]], [[Dyalog APL]], [[APLX]], [[ngn/APL]], [[GNU APL]]}}
=== Operator or function? ===
The syntax <source lang=apl inline>a / b</source> is ambiguous: it may be an invocation of a [[dyadic function]] <source lang=apl inline>/</source> with left [[argument]] <source lang=apl inline>a</source> and right argument <source lang=apl inline>b</source>, or of a [[monadic operator]] with [[operand]] <source lang=apl inline>a</source> and right argument <source lang=apl inline>b</source>. In early APLs there was no way to resolve this ambiguity, but with the extension of [[operator]]s to allow arbitrary function operands instead of a specified set of [[primitive function]]s, the distinction becomes apparent: a function Replicate can be used as an [[operand]] while an operator Replicate cannot.
One test of Replicate's nature is to try Replicate [[Each]]<ref>Benkard, J. Philip. [https://dl.acm.org/doi/10.1145/384282.28345 "Replicate each, anyone?"]. [[APL87]].</ref> with an expression such as <source lang=apl inline>1 3 /¨ 'ab' 'cd'</source>. If Replicate is implemented as an operator, it will be applied to the operand <source lang=apl inline>1 3</source>, and Each will be applied to the resulting [[derived function]] <source lang=apl inline>1 3/</source>.
<source lang=apl>
      1 3 /¨ 'ab' 'cd' 
abbb  cddd
      (1 3/)¨ 'ab' 'cd'
abbb  cddd
</source>{{Works in|[[SHARP APL]] (with <source lang=apl inline>¨></source> in place of <source lang=apl inline>¨</source>), [[APL2]], [[APLX]]}}
If Replicate is a function, then Each will apply to Replicate only, and the resulting derived function will be invoked monadically.
<source lang=apl>
      1 3 /¨ 'ab' 'cd' 
ab  cccddd
      1 3 (/¨) 'ab' 'cd'
ab  cccddd
</source>{{Works in|[[NARS2000]], [[Dyalog APL]], [[GNU APL]]}}
In early APLs such as [[APL\360]], applying an operator to Compress will always result in a [[SYNTAX ERROR]], because Compress is not an allowed operand of any operator. This is also the case in [[ngn/APL]]: although operators can apply to any function, Replicate cannot be used unless both arguments are immediately available. In both cases there is no way to determine whether Replicate "acts like a function" or "acts like an operator".


== History ==
== History ==

Navigation menu