emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

literal functions [was: Re: dash.el]


From: Joost Kremers
Subject: literal functions [was: Re: dash.el]
Date: Wed, 13 May 2020 09:41:14 +0200
User-agent: mu4e 1.4.4; emacs 27.0.91


On Wed, May 13 2020, Richard Stallman wrote:
> In Clojure, a function literal is written with `#(...)`, and > it > supports `%n` for arguments, where n=1,2,3... (Actually, I > don't > know if Clojure supports `%10` and up or if it just goes up > to > `%9`, but I guess that doesn't matter much). A single `%` is > synonymous with `%1`, which is used when there's only one > argument. Also supported is `%&`, which is similar to `&rest > <var>` in Elisp, being bound to a list containing the > remaining > arguments.

This is rather shell-like.  Can we find a nicer one?

Not sure what a nicer syntax would look like. If nicer means more verbose, function literals might become pointless, because their compactness is IMHO the main reason for having them.

Using Unicode would be nice:

   (seq-reduce λ(* 2 (+ α β)) (number-sequence 1 10) 0)

But that runs into obvious problems (font support, input method). Personally, I wouldn't be adverse to adopting the Clojure syntax for function literals wholesale, except that `#(...)` already has a meaning for the reader. Perhaps `#f(...)` can be used instead, since it follows the existing pattern (`#` plus a single character).

It doesn't specify how many args the literal function should accept. Maybe there is no need for that, but if we want to do that, how could we?

Something like

   #f2(* 2 (+ %1 %2))

with the 2 after #f indicating the number of arguments? Doable, I guess. But I'm not sure it gets you anything, unless you also find a way to indicate the use of an `&rest` argument. If you don't, you'd need to go through the entire form anyway to search for it, and then you might as well collect the other arguments as well.

You could of course decide not to support `&rest` arguments. That would limit a function literal's usefulness somewhat, but that might be an acceptable compromise.

OTOH you might want to use gensyms to represent the arguments in the expanded lambda form. In that case you need to go through the entire form as well, so no need to indicate the number of arguments.

A related question is what to do about function literals such as `#f(* 2 (+ %1 %3))`. If the reader collects all %n arguments and puts them in the argument list in the right order, this doesn't have to be an error. OTOH if you use the #f2 syntax and let the reader create an argument list without checking, a form such as , `#f2(* 2 (+ %1 %3))` would yield a void variable error upon execution.

My personal preference would be to just use `#f(...)` and not indicate the number of arguments.

Is there ever a need for nested literal functions? To implement them
fully, we would want to use different arg names at each level.

Which is probably why Clojure doesn't allow nesting them. Function literals are not meant to replace lambdas in every context, they are mainly meant as short, one-off function arguments to higher-order predicates, so it's an acceptable compromise that they're not nestable.
Does anyone think this is a bad idea?

I obviously don't. :-)

--
Joost Kremers
Life has its moments



reply via email to

[Prev in Thread] Current Thread [Next in Thread]