justin2004
justin2004

Reputation: 305

APL selective assignment with commute

Selective assignment (with replicate on the left) works as expected:

  d←'bob'
  (1 0 0/d)←'B'
  d

Bob

But the same thing (though expressed with a commute) does not:

  d←'bob'
  (d/⍨1 0 0)←'B'
SYNTAX ERROR
  (d/⍨1 0 0)←'B'
            ∧

I expected the same result as the first selective assignment. Why is this a syntax error?

Upvotes: 1

Views: 63

Answers (1)

B. Wilson
B. Wilson

Reputation: 131

The narrow answer is that selective assignment is simply defined that way, (see the docs), i.e. the name to be assigned must be the rightmost token in the overall left expression.

The following expressions begin to hint at why such a limitation exists:

d←'bob' ⋄ m←1 0 0
(m/d)←'B'
(d/⍨m)←'R'

Essentially, one issue is that you need a way to disambiguate the assignment target from all the other names used in the left expression. The above example could be disambiguated in the interpreter with special code, but what about (((a/b)/⍨c)/d)←... or more gnarly expressions?

Actually, you can sort of solve the above with a Prolog-like solution search, but at that point you've legitimately embedded Prolog in APL. However, the most broadly construed selective assignment is even broader than that, allowing arbitrary expressions as the goal to be unified, which runs headlong into the halting problem.

Anyway, all this just means that there's no way we can get around the need to put arbitrary limitations on selective assignment-like syntaxes, and in the really narrow case of allowing a single Commute, we gain very little in language expressivity for it.

Upvotes: 2

Related Questions