rslt ← {larg} (func ##.tc) rarg ⍝ Trace of function application.
Monadic operator [tc] applies its [func]tion operand to/between its argument(s).
As a side-effect, the arguments, function and result are displayed in the sess-
ion.
[tc] can be used as a debugging aid and to observe the behaviour of primitive or
defined operators.
"... for it [correcting code] is damnably troublesome work, & plagues me." Ada,
Countess of Lovelace in a letter to Charles Babbage, 6 July 1843.
In the case of a _primitive_ operator, [tc] shows its _formal_ reduction sequ-
ence. When unobserved by [tc], the interpreter is free to "cheat" in a number of
ways: for example, +/ of a boolean array is actually performed 8-items-at-a-time
and for associative functions such as + and ×, scan (\) operates cumulatively in
one pass from left to right.
Technical note:
tc←{⍺←⊢ ⍝ Trace of function application.
fn←⍺⍺{⍙←⍺⍺ ⋄ ~∘' ⍙'⍕⊂⎕OR'⍙'}⍵ ⍝ function representation.
↑⊢/⎕←⍺ fn ⍵'=>',⊂⍺ ⍺⍺ ⍵ ⍝ display and result.
}
As '⍺⍺' is outside the domain of ⎕OR, the function representation is produced by
naming the operand function within an inner operator.
fn←⍺⍺{⍙←⍺⍺ ⋄ ········⎕OR'⍙'}⍵ ⍝ function representation.
The other part of this inner function:
fn←⍺⍺{·······~∘' ⍙'⍕⊂······}⍵ ⍝ function representation.
Removes the assigned name (⍙) and superfluous blanks from the format of the ⎕OR
of a defined function, leaving it represented by a single ∇ character.
Examples:
1 2 3 +tc 4 5 6 ⍝ vector addition.
1 2 3 + 4 5 6 => 5 7 9
5 7 9
1 2 3 +tc¨ 4 5 6 ⍝ item-wise addition.
1 + 4 => 5
2 + 5 => 7
3 + 6 => 9
5 7 9
⍳tc¨2 3 ⍝ monadic function application.
⍳ 2 => 1 2
⍳ 3 => 1 2 3
┌───┬─────┐
│1 2│1 2 3│
└───┴─────┘
×tc/⍳5 ⍝ reduction
4 × 5 => 20
3 × 20 => 60
2 × 60 => 120
1 × 120 => 120
120
×tc\⍳5 ⍝ scan
1 × 2 => 2
2 × 3 => 6
1 × 6 => 6
3 × 4 => 12
2 × 12 => 24
1 × 24 => 24
4 × 5 => 20
3 × 20 => 60
2 × 60 => 120
1 × 120 => 120
1 2 6 24 120
0 +tc foldl ⍳5 ⍝ +foldl: fold from left
0 + 1 => 1
1 + 2 => 3
3 + 3 => 6
6 + 4 => 10
10 + 5 => 15
15
+tc/⍳5 ⍝ compare with reduction (fold from right).
4 + 5 => 9
3 + 9 => 12
2 + 12 => 14
1 + 14 => 15
15
{⍵ ⍵}tc¨⍳3 ⍝ non-primitive operand represented as '∇'
∇ 1 => 1 1
∇ 2 => 2 2
∇ 3 => 3 3
┌───┬───┬───┐
│1 1│2 2│3 3│
└───┴───┴───┘
See also: foldl scan
Back to: contents
Back to: Workspaces