Match: Difference between revisions

Jump to navigation Jump to search
1,192 bytes added ,  22:31, 10 September 2022
m
Text replacement - "<source" to "<syntaxhighlight"
m (Text replacement - "{{APL built-ins}}" to "{{APL built-ins}}Category:Primitive functions")
m (Text replacement - "<source" to "<syntaxhighlight")
 
(10 intermediate revisions by 3 users not shown)
Line 4: Line 4:


Match can be used to compare [[simple]] [[vector]]s to see if they are the same:
Match can be used to compare [[simple]] [[vector]]s to see if they are the same:
<source lang=apl>
<syntaxhighlight lang=apl>
       1 2 3 ≡ 1 2 3
       1 2 3 ≡ 1 2 3
1
1
       1 2 3 ≡ 1 2 5
       1 2 3 ≡ 1 2 5
0
0
</source>
</syntaxhighlight>
Using [[strand notation]] to create [[nested]] arrays, we see that Match also compares elements nested within arrays:
Using [[strand notation]] to create [[nested]] arrays, we see that Match also compares elements nested within arrays:
<source lang=apl>
<syntaxhighlight lang=apl>
       (1 2)3 ≡ (2 1)3
       (1 2)3 ≡ (2 1)3
0
0
</source>
</syntaxhighlight>
Arrays with the same elements but different shapes do not match. Unlike [[Equal to]], Match never gives a [[RANK ERROR]] or [[LENGTH ERROR]] because of argument shapes.
Arrays with the same elements but different shapes do not match. Unlike [[Equal to]], Match never gives a [[RANK ERROR]] or [[LENGTH ERROR]] because of argument shapes.
<source lang=apl>
<syntaxhighlight lang=apl>
       (3 2⍴⍳6) ≡ (2 3⍴⍳6)
       (3 2⍴⍳6) ≡ (2 3⍴⍳6)
0
0
       'a' ≡ ,'a'  ⍝ Scalar versus singleton
       'a' ≡ ,'a'  ⍝ Scalar versus singleton
0
0
</source>
</syntaxhighlight>
Match depends on [[comparison tolerance]]. Here two arrays which are not exactly identical are still reported as matching:
Match depends on [[comparison tolerance]]. Here two arrays which are not exactly identical are still reported as matching:
<source lang=apl>
<syntaxhighlight lang=apl>
       a←⎕CT+b←2 3 4  ⍝ Two vectors
       a←⎕CT+b←2 3 4  ⍝ Two vectors
       a - b  ⍝ Difference is not zero
       a - b  ⍝ Difference is not zero
Line 31: Line 31:
       a {⎕CT←0⋄⍺≡⍵} b  ⍝ ...but not intolerantly
       a {⎕CT←0⋄⍺≡⍵} b  ⍝ ...but not intolerantly
0
0
</source>
</syntaxhighlight>


== Description ==
== Description ==
Line 43: Line 43:
=== APL model ===
=== APL model ===


The following model implements Match in [[Dyalog APL]]. Because Match compares the same characteristic of both arguments, [[Over]] (<source lang=apl inline>⍥</source>) is used throughout.
The following model implements Match in [[Dyalog APL]]. Because Match compares the same characteristic of both arguments, [[Over]] (<syntaxhighlight lang=apl inline>⍥</syntaxhighlight>) is used throughout.
<source lang=apl>
<syntaxhighlight lang=apl>
Match ← {
Match ← {
   ⍺≠⍥(≢⍴)⍵: 0    ⍝ Rank
   ⍺≠⍥(≢⍴)⍵: 0    ⍝ Rank
Line 52: Line 52:
   ⍺∧.∇⍥,⍵        ⍝ Recurse
   ⍺∧.∇⍥,⍵        ⍝ Recurse
}
}
</source>
</syntaxhighlight>
{{Works in|[[Dyalog APL 18.0]]}}
{{Works in|[[Dyalog APL 18.0]]}}


== History ==
Match was defined in [[Jim Brown]]'s 1971 Ph.D. thesis, "A Generalization of APL",<ref>[[Jim Brown]]. [http://www.softwarepreservation.org/projects/apl/Books/AGENERALIZATIONOFAPL "A Generalization of APL"]. 1971.</ref> with the name "same". It was implemented in both [[NARS]] (called it "Equivalent") and [[SHARP APL]]<ref>[https://www.jsoftware.com/papers/satn41.htm "Composition and Enclosure"]. SATN-41, 1981-06-20.</ref> in 1981. Both [[Dyalog APL]] and [[APL2]] included it with the name "Match" taken from SHARP, which has generally been used in later APLs as well.
== External links ==
=== Documentation ===
* [https://help.dyalog.com/latest/#Language/Primitive%20Functions/Match.htm Dyalog]
* [http://microapl.com/apl_help/ch_020_020_360.htm APLX]
* [https://mlochbaum.github.io/BQN/doc/match.html BQN]
* J [https://www.jsoftware.com/help/dictionary/d122.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/minusco#dyadic NuVoc] (as <syntaxhighlight lang=j inline>-:</syntaxhighlight>)
== See also ==
* [[Not Match]]
== References ==
<references/>
{{APL built-ins}}[[Category:Primitive functions]]
{{APL built-ins}}[[Category:Primitive functions]]

Navigation menu