Find: Difference between revisions

Jump to navigation Jump to search
72 bytes added ,  21:01, 10 September 2022
m
Text replacement - "</source>" to "</syntaxhighlight>"
(Add model that reflects common implementation.)
m (Text replacement - "</source>" to "</syntaxhighlight>")
Line 27: Line 27:
       WEEK⍷'DAY'  ⍝ WEEK not found in 'DAY'; left arg may have higher rank but it is never found
       WEEK⍷'DAY'  ⍝ WEEK not found in 'DAY'; left arg may have higher rank but it is never found
0 0 0
0 0 0
</source>
</syntaxhighlight>


For [[nested array|nested arrays]], Find tests for exact [[match]] between the elements.
For [[nested array|nested arrays]], Find tests for exact [[match]] between the elements.
Line 34: Line 34:
       'BIRDS' 'NEST'⍷'BIRDS' 'NEST' 'SOUP'
       'BIRDS' 'NEST'⍷'BIRDS' 'NEST' 'SOUP'
1 0 0
1 0 0
</source>
</syntaxhighlight>


== Model ==
== Model ==
Line 46: Line 46:
  (⍴⍵) ↑ ⍺∘{⍺≡(⍴⍺)↑⍵↓ww}¨ ⍳(×⍴⍺)+(⍴⍵)-⍴⍺
  (⍴⍵) ↑ ⍺∘{⍺≡(⍴⍺)↑⍵↓ww}¨ ⍳(×⍴⍺)+(⍴⍵)-⍴⍺
}
}
</source>
</syntaxhighlight>
=== Empty left argument ===
=== Empty left argument ===
Implementations differ in their treatment of empty left arguments:
Implementations differ in their treatment of empty left arguments:
Line 53: Line 53:
* [[APL+]] finds empty arrays everywhere, even where they would extend beyond the edges of the right argument.
* [[APL+]] finds empty arrays everywhere, even where they would extend beyond the edges of the right argument.


The prototype is never used, in contrast to [[Match]], which in many APLs compares prototypes of empty arrays. The behavior may come from the use of the [[inner product]] <source lang=apl inline>∧.=</source> in early [[Array_model#Flat_array_theory|flat]] APLs where [[Match]] is not a primitive; this function naturally checks elements and not the prototype. [[Adin Falkoff]] presented code for Find-like functions using <source lang=apl inline>∧.=</source> at [[APL79]].<ref>[[Adin Falkoff|Falkoff, Adin]]. [https://dl.acm.org/doi/10.1145/390009.804448 A note on pattern matching: Where do you find the match to an empty array?] at [[APL79]].</ref>
The prototype is never used, in contrast to [[Match]], which in many APLs compares prototypes of empty arrays. The behavior may come from the use of the [[inner product]] <source lang=apl inline>∧.=</syntaxhighlight> in early [[Array_model#Flat_array_theory|flat]] APLs where [[Match]] is not a primitive; this function naturally checks elements and not the prototype. [[Adin Falkoff]] presented code for Find-like functions using <source lang=apl inline>∧.=</syntaxhighlight> at [[APL79]].<ref>[[Adin Falkoff|Falkoff, Adin]]. [https://dl.acm.org/doi/10.1145/390009.804448 A note on pattern matching: Where do you find the match to an empty array?] at [[APL79]].</ref>


Correspondingly, the above model can be amended to match the behaviour of the primitive (in APL2, etc.) by replacing the <source lang=apl inline>≡</source> with <source lang=apl inline>{(⍺≡⍥⍴⍵)∧(∧/⍺≡¨⍥,⍵)}</source> which ignores prototypes, only comparing shape and elements:<ref>[[Adám Brudzewsky|Brudzewsky, Adám]]. [https://forums.dyalog.com/viewtopic.php?f=30&t=1735&p=7416#p7416 RE: ⍷ follies]. Dyalog Forums. 23 Aug 2022.</ref>
Correspondingly, the above model can be amended to match the behaviour of the primitive (in APL2, etc.) by replacing the <source lang=apl inline>≡</syntaxhighlight> with <source lang=apl inline>{(⍺≡⍥⍴⍵)∧(∧/⍺≡¨⍥,⍵)}</syntaxhighlight> which ignores prototypes, only comparing shape and elements:<ref>[[Adám Brudzewsky|Brudzewsky, Adám]]. [https://forums.dyalog.com/viewtopic.php?f=30&t=1735&p=7416#p7416 RE: ⍷ follies]. Dyalog Forums. 23 Aug 2022.</ref>
<source lang=apl>
<source lang=apl>
ebar2←{⎕IO←0
ebar2←{⎕IO←0
Line 64: Line 64:
  (⍴⍵) ↑ ⍺∘{⍺ {(⍺≡⍥⍴⍵)∧(∧/⍺≡¨⍥,⍵)} (⍴⍺)↑⍵↓ww}¨ ⍳(×⍴⍺)+(⍴⍵)-⍴⍺
  (⍴⍵) ↑ ⍺∘{⍺ {(⍺≡⍥⍴⍵)∧(∧/⍺≡¨⍥,⍵)} (⍴⍺)↑⍵↓ww}¨ ⍳(×⍴⍺)+(⍴⍵)-⍴⍺
}
}
</source>
</syntaxhighlight>
== See also ==
== See also ==
* [[Membership]]
* [[Membership]]

Navigation menu