Tacit programming: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
Tacit functions apply to implicit arguments following a small set of rules. This is in contrast to the explicit use of arguments in [[dfns]] (<source inline lang=apl>⍺ ⍵</source>) and [[tradfns]] (which have named arguments). Known dialects which implement trains are Dyalog APL, dzaima/apl, ngn/apl and NARS2000.
Tacit functions apply to implicit arguments following a small set of rules. This is in contrast to the explicit use of arguments in [[dfns]] (<source inline lang=apl>⍺ ⍵</source>) and [[tradfns]] (which have named arguments). Known dialects which implement trains are [[Dyalog APL]], [[dzaima/APL]], [[ngn/apl]] and [[NARS2000]].


== Primitives ==
== Primitives ==

Revision as of 12:02, 9 January 2020

Tacit functions apply to implicit arguments following a small set of rules. This is in contrast to the explicit use of arguments in dfns (⍺ ⍵) and tradfns (which have named arguments). Known dialects which implement trains are Dyalog APL, dzaima/APL, ngn/apl and NARS2000.

Primitives

All primitive functions are tacit. Some APLs allow primitive functions to be named.

      plus ← +
      times ← ×
      6 times 3 plus 5
48

Derived functions

Functions derived from an operator and operand are tacit.

      sum ← +/
      sum ⍳10
55

Trains

A train is a series of functions in isolation. An isolated function is either surrounded by parentheses or named. Arguments are processed by the following rules:

A 2-train is an atop:

  (g h) ⍵ ⬄ g (  h ⍵)
⍺ (g h) ⍵ ⬄ g (⍺ h ⍵)

A 3-train is a fork:

  (f g h) ⍵ ⬄ (  f ⍵) g (  h ⍵)
⍺ (f g h) ⍵ ⬄ (⍺ f ⍵) g (⍺ h ⍵)

The left tine of a fork (but not an atop) can be an array:

  (A g h) ⍵ ⬄ A g (  h ⍵)
⍺ (A g h) ⍵ ⬄ A g (⍺ h ⍵)

Examples

One of the major benefits of tacit programming is the ability to convey a short, well-defined idea as an isolated expression. This aids both human readability (semantic density) and the computer's ability to interpret code, potentially executing special code for particular idioms.

Plus and minus

      (+,-)2
2 ¯2
      1 2 3 (+,-) 4
5 6 7 ¯3 ¯2 ¯1
      (2 3⍴0) (+,-) 1
1 1 1 ¯1 ¯1 ¯1
1 1 1 ¯1 ¯1 ¯1

Arithmetic mean

      (+⌿÷≢) ⍳10       ⍝ Mean of the first ten integers
5.5
      (+⌿÷≢) 5 4⍴⍳4    ⍝ Mean of columns in a matrix
1 2 3 4

Top-heavy fraction as decimal

      (1∧⊢,÷) 1.125
9 8

Is it a palindrome?

      (⌽≡⊢)'racecar'
1
      (⌽≡⊢)'racecat'
0

Split delimited text

      ','(≠⊆⊢)'comma,delimited,text'
┌─────┬─────────┬────┐
│comma│delimited│text│
└─────┴─────────┴────┘
      ' '(≠⊆⊢)'space delimited text'
┌─────┬─────────┬────┐
│space│delimited│text│
└─────┴─────────┴────┘

Component of a vector in the direction of another vector

Sometimes a train can make an expression nicely resemble its equivalent definition in traditional mathematical notation. As an example, here is a program to compute the component of a vector a in the direction of another vector b.

      Sqrt ← *∘.5              ⍝ Square root 
      Norm ← Sqrt+.×⍨          ⍝ Magnitude (norm) of numeric vector in Euclidean space
      Unit ← ÷∘Norm⍨           ⍝ Unit vector in direction of vector ⍵
      InDirOf ← (⊢×+.×)∘Unit   ⍝ Component of vector ⍺ in direction of vector ⍵
      3 5 2 InDirOf 0 0 1      ⍝ Trivial example
0 0 2

In particular, the definition of InDirOf resembles the definition in traditional mathematical notation:

Traditional notation APL
(Sqrt+.×⍨) b
(÷∘Norm⍨) b
a +.× b
a (⊢×+.×)∘Unit b


APL features [edit]
Built-ins Primitives (functions, operators) ∙ Quad name
Array model ShapeRankDepthBoundIndex (Indexing) ∙ AxisRavelRavel orderElementScalarVectorMatrixSimple scalarSimple arrayNested arrayCellMajor cellSubarrayEmpty arrayPrototype
Data types Number (Boolean, Complex number) ∙ Character (String) ∙ BoxNamespaceFunction array
Concepts and paradigms Conformability (Scalar extension, Leading axis agreement) ∙ Scalar function (Pervasion) ∙ Identity elementComplex floorArray ordering (Total) ∙ Tacit programming (Function composition, Close composition) ∙ GlyphLeading axis theoryMajor cell searchFirst-class function
Errors LIMIT ERRORRANK ERRORSYNTAX ERRORDOMAIN ERRORLENGTH ERRORINDEX ERRORVALUE ERROREVOLUTION ERROR
APL syntax [edit]
General Comparison with traditional mathematicsPrecedenceTacit programming (Train, Hook, Split composition)
Array Numeric literalStringStrand notationObject literalArray notation (design considerations)
Function ArgumentFunction valenceDerived functionDerived operatorNiladic functionMonadic functionDyadic functionAmbivalent functionDefined function (traditional)DfnFunction train
Operator OperandOperator valenceTradopDopDerived operator
Assignment MultipleIndexedSelectiveModified
Other Function axisBracket indexingBranchStatement separatorQuad nameSystem commandUser commandKeywordDot notationFunction-operator overloadingControl structureComment