From: Taylan Kammer <taylan.kammer@gmail.com>
To: spacecadet <spacecadet@purge.sh>, guile-devel@gnu.org
Subject: Re: Lexically bound macro, with lexically bound transformer
Date: Thu, 25 Jul 2024 11:50:59 +0200 [thread overview]
Message-ID: <26582976-7f8b-4297-b7ec-15a87c83955c@gmail.com> (raw)
In-Reply-To: <543f985e-eeeb-44a2-d376-af1576710070@purge.sh>
On 24.07.2024 19:10, spacecadet wrote:
>> I think the guile-user list might be more appropriate.
>
> Noted
>
>> (let-syntax ((outer (lambda (x) #'(+ 1 2))))
>> (let-syntax ((inner (lambda (x) (outer x))))
>> (inner)))
>
> That works, but I guess this isn't possible then
>
> (lambda* (#:key outer)
> (let-syntax ((inner outer))
> (inner ...)))
>
> Since that would require evaluating run-time code at compile-time
>
> Is there a way to write a macro that's expanded at run-time?
>
> It feels like I'm asking the wrong question now, but I'm
> really looking for a way to bind macros at run-time
> so they can be used functionally
>
> I guess the right answer is to use functions instead of macros?
Yeah, it's generally a good idea to reserve macros for things that regular procedures (functions) simply can't do.
Many things where one thinks of using a macro can be done using a higher-order functions instead. (I.e. a procedure that takes another procedure as an argument and executes it in some special context.)
For example, let's say I want a way to time (benchmark) code. I could create a macro to do it like this:
;; Example code only, may not work properly
(define-syntax time
(syntax-rules ()
((_ body ...)
(let ((start-time (current-time)))
body ...
(- (current-time) start-time)))))
(time
(do-thing)
(do-other-thing)
(very-complicated-thing))
Or I could just use a higher-order function:
(define (time thunk)
(let ((start-time (current-time)))
(thunk)
(- (current-time) start-time)))
(time
(lambda ()
(do-thing)
(do-other-thing)
(very-complicated-thing)))
--
Taylan
next prev parent reply other threads:[~2024-07-25 9:50 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-24 1:06 Lexically bound macro, with lexically bound transformer spacecadet
2024-07-24 11:15 ` Taylan Kammer
2024-07-24 17:10 ` spacecadet
2024-07-24 19:54 ` Attila Lendvai
2024-07-25 9:50 ` Taylan Kammer [this message]
2024-07-26 7:56 ` Maxime Devos
2024-07-27 0:30 ` spacecadet
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=26582976-7f8b-4297-b7ec-15a87c83955c@gmail.com \
--to=taylan.kammer@gmail.com \
--cc=guile-devel@gnu.org \
--cc=spacecadet@purge.sh \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).