💾 Archived View for idiomdrottning.org › fn-and-over captured on 2024-08-18 at 19:03:20. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2023-01-29)
-=-=-=-=-=-=-
Tired of writing the same basic, boring lambda bindings over and over again.
Let’s automate that.
(fn (cons x rest))
is shorthand for (lambda (x . rest) (cons x rest))
and
(fn (print y) (+ x z))
is shorthand for (lambda (x y z) (print y) (+ x z)).
(fn body ...)
is shorthand for
(lambda some-basic-bindings body ...)
where some-basic-bindings is one of
and the fn macro automatically figures out which of those four you mean. Note that the length of rest will be different based on the presence of y or z.
The function can refer to itself as f.
(In other words, if the body refers to f, the expansion gets wrapped in (letrec ((f …)) f).)
(over body ...)
is shorthand for
(cut map (lambda some-basic-bindings body ...) <>)
except that the map can take any number of lists and that i is also anaphorically bound to the list index in body.
Here is an example:
((over (+ x x y i)) '(10 20 40) '(3 6 9))
⇒ (23 47 91)
Like fn, the function can refer to itself as f.
((over (display x) (eif x (f (sub1 x)) x)) '(2 4 3))
displays 210432103210 and returns (0 0 0).
fn and over are part of the brev-separate egg for chicken. For source code,
git clone https://idiomdrottning.org/brev-separate
Thanks to Chris Brannon and Juergen Lorenz for the idea.