3

Say I want to name a function (assign to a variable) that does n-wise (2 in this example) reduction. Using tryapl.org it looks like I can't:

      v←⍳5
      2,/v
┌───┬───┬───┬───┐
│1 2│2 3│3 4│4 5│
└───┴───┴───┴───┘
      2,/
SYNTAX ERROR: Missing right argument
      2,/
       ∧

When I evaluate 2,/ I expect to get a little ASCII train tree something like this:

      -,/
┌┴┐
- /
┌─┘
,  

It seems like 2,/ works as an expression but not as a nameable function -- is that right?

justin2004
  • 75
  • 5

1 Answers1

4

Yes, but it has to form one of two patterns:

  1. A 3-train (fork) Agh
  2. A function with a bound argument A∘g

A 3-train (fork) Agh

This is an array A, a function g, and a function h, where h is applied monadically to the overall argument, and g is applied dyadically to the result of g, with A as g's left argument. Since ,/ is dyadic with the 2 as left argument, we have an extra function h pre-processing the argument. We don't need any pre-processing, so we use an identity function:

      v←⍳5
      f←2,/⊢
      f
┌─┼──┐
2 ,/ ⊢
      f v
┌───┬───┬───┬───┐
│1 2│2 3│3 4│4 5│
└───┴───┴───┴───┘

More about Forks on APL Wiki.

A function with a bound argument A∘g

Here, we use the Bind operator to curry a constant argument to a dyadic function. Our constant is 2 and the function g is ,/ but since operators like / have long left scope 2∘,/ would be parsed as (2∘,)/ which isn't what we want, so we have to parenthesise ,/:

      v←⍳5
      f←2∘(,/)
      f
 ∘
┌┴┐
2 ,/
      f v
┌───┬───┬───┬───┐
│1 2│2 3│3 4│4 5│
└───┴───┴───┴───┘

More about Bind on APL Wiki.

Adám
  • 6,573
  • 20
  • 37