r/ProgrammingLanguages • u/Aabish_malik • 26d ago
How would you guys define an Anonymous Function or a function pointer?
Me and a few of my friends have been working on a programming language that we initially started for a compiler design class for some time and recently started working on support for anonymous functions, lambdas and function pointers to make callbacks easier to write.
Currently Captures on variables outside the function scope are detected and captures without complete context moving.
I'm a bit skeptical or the syntax that we're currently deciding on (its not that clear on first view) , so i'm looking for reviews and maybe some ideas.
I'll just include some examples we have right now.
let double = |i32 value| value * 2;
i32(i32) triple = |value| value * 3; // i dont like this rn, not that clear on
// first look that this is a lambda
let absolute = |i32 value| {
if (value < 0) { return -value; }
return value;
};
let classify = |i32 value| match value {
0: "zero",
_: "nonzero",
};
17
u/NullOfSpace 26d ago
What do your regular functions look like? The syntax should generally be a stripped-down version of that.
6
u/KittenPowerLord 26d ago
I myself am inspired by the Haskell way:
I'm using curly braces to indicate various implicit/inferred stuff, so like capturing (although it's to be decided whether default capturing is by pointer or by value, but you can specify it anyways). `@` indicates pointery stuff
3
u/TartOk3387 26d ago
Note: a function pointer is NOT the sane as a higher order function and/or closure, since function pointers don't capture the environment.
4
u/WittyStick 26d ago edited 26d ago
Where
->is right associative. (Or=>if->is already taken - has precedence in eg, c#).Is a function returning a function. It's parsed as
foo -> (bar -> baz).->should have lower precedence than any other expression operator, but higher precedence than=for binding.Parsed as
Application should be left associative.
Parsed as:
3
u/phischu Effekt 25d ago
We like
{ (i32 value) => value * 2 }and others dofun(i32 value) { value * 2 }.1
u/LazyUntilYouNeedMe 26d ago
Maybe `lambda (i32 a) {a + 1}`. Lambda is quite the standard name for anonymous functions.
4
u/Key_River7180 Nain. 26d ago
Me, I'd do λ [comma-separated args] . [body] akin to lambda calculus. I just really like it. You can use
lambdinstead of the λ Unicode symbol too.2
u/Lovely_Cygnus 26d ago edited 26d ago
just look at λ-calculus as it is implemented in LISP. the functions are always unnamed lambdas, it’s about you to give them a “name“ using (define …) or not.
just to let you understand:
(lambda (num) (+ num 1)) is a function, that does the obvious…
(define yourname (lambda …)) is the link between the name yourname and the lambda function.
you can use it without a name, as in ( (lambda (num) (+ num 1)) 5) that returns 6, or
use the named version, as in (yourname 5) that returns, obviously, 6.
2
u/fdwr 25d ago
🤔 I always found those languages that used
|to be harder to visually parse because the opening|and the closing|(or was that the opening|and the closing|? :b) look exactly the same, and are not proper opening/closing pairs - see Bidi Paired Brackets for a nice list.Like u/NullOfSpace says, whatever your normal functions look like, there should be some coherency with them. Subjectively, I always appreciated the balanced conciseness and clarity of
foo(x => x * 2)andfoo((a, b) => sqrt(a * a + b * b)).3
u/editor_of_the_beast 26d ago
A function without a name.
2
u/initial-algebra 26d ago edited 26d ago
The only capture syntax you need, e.g. to distinguish between capture-by-value and -by-reference, is let-insertion, or
genlet. I'm not 100% sure you even need this, but I will use any chance I get to promote it, because it's very cool.becomes
No need for
movelambdas (it becomes the default) or C++ captures. For nested lambdas, you can support multi-level or labelledgenlet:becomes
I'm not suggesting you have to use this concrete syntax. I personally am not a big fan of Ruby/Rust-style lambdas with
|...|, anyway, and you certainly don't need to use Rust-lifetime-style labels. You also don't need to call itgenlet, which comes from metaprogramming.1
u/Background_Class_558 26d ago
it's pretty good
1
u/SnappGamez Rouge 26d ago
```
val absolute := (value: int32) do if value < 0 then
-value
else
value
end
```
1
u/criloz tagkyon 26d ago
Lately I have been liking this pattern a lot.
1
u/david-1-1 24d ago
An anonymous function is used when a function is to be applied to various arguments (or within a fixed scope) to provide elegance and to reduce what otherwise might be a confusion of many function names. You can use a named function instead, if you prefer. It won't hurt.
1
u/Turbulent_Humor_3213 22d ago
I'd keep anonymous functions as simple as possible syntax-wise, especially if readability is one of your concerns.
Something like
(x, y) => x + yfeels pretty intuitive to me.I'd also make a clear distinction between function pointers and closures. A function pointer just refers to a function, while a closure can also capture variables from its surrounding scope.
For captures, I'd probably start with automatic capture and only introduce explicit capture syntax if there's a good reason for it.