How do I pass a function as an argument?
The basic idea is something like this (which doesn't work):
∇R ← double a
R ← 2 × a
∇
∇R ← a applytwice f
R ← f f a
∇
5 applytwice double
Is there something like \fun
in erlang or function-pointers in C?
In APL, functions may not be passed as arguments to functions. However, APL has operators, which are higher order functions, that can take functions as arguments. There are primitive operators like / (reduction) used for example to sum up a vector +/v. The function + is the left operand and is passed into the operator /.
In Dyalog APL, there is a primitive operator using the (named "power") for apply a function n times so we can write:
double←{2×⍵}
(double ⍣ 2) 7
28
(double ⍣ 10) 7
7168
You can also write your own operators (in most APLs). In Dyalog APL we can write your applytwice operator as:
applytwice←{⍺⍺ ⍺⍺ ⍵}
double applytwice 7
28
Finally, you can pass functions around by putting them in a namespace and passing the namespace around instead. This is like a very light weight instance of class with a method. For example:
s←⎕NS ''
s.f←{2×⍵}
ApplyFTwice←{⍺.f ⍺.f ⍵}
s ApplyFTwice 7
28
In this case, the function has to be named f, but we could many different functions named f, each in its own namespace.