1

Consider the following function, which should return a function that adds two to any given argument:

∇r←addtwo
  r←{⍵+2}
∇

This code loads without any errors, but I just cannot use the return value without causing errors.

      addtwo ⍝ doesn't cause errors
      addtwo 1
VALUE ERROR
      addtwo 1
      ^
      x←addtwo
VALUE ERROR
      x←addtwo
      ^

I am using GNU-APL 1.8.

rajashekar
  • 3,460
  • 11
  • 27

2 Answers2

2

The usual work-around for what you intend to do is to return a string from the function and execute (⍎) the string:

      ∇Z←FOO
[1] ⍝ return a string that can be ⍎'ed····
[2] Z←'{⍵+2}'
[3] ∇

      ⍎FOO,⍕42
44
  • This is useful for constant dfns, but when I want to use it as a closure and capture variables at runtime, it will become complicated. – rajashekar Dec 31 '20 at 12:04
  • 1
    The constants were only used to simplify the example. Apart from that, using closures in APL is, IMHO, like writing web pages in FORTRAN - it is possible but probably the wrong way of using the language. – user13674863 Dec 31 '20 at 14:41
1

I believe Dyalog APL is the only implementation allowing this. Try it online!

Even though this could work, it isn't the normal APL way of doing things. You may want to look into writing your own operator instead.

Adám
  • 6,573
  • 20
  • 37