Is it possible to return lambdas in a function?

126 views Asked by At

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.

2

There are 2 answers

0
Adám On BEST ANSWER

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.

2
user13674863 On

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