-- Leo's gemini proxy

-- Connecting to idiomdrottning.org:1965...

-- Connected

-- Sending request

-- Meta line: 20 text/gemini; lang=en

fn and over


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


(fn body ...)

is shorthand for


(lambda some-basic-bindings body ...)

where some-basic-bindings is one of


args

(x . rest)

(x y . rest)

(x y z . rest)


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


(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).


Implementation


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.

-- Response ended

-- Page fetched on Sat Apr 20 01:59:31 2024