From: Jakub Jankiewicz <jcubic@onet.pl>
To: Michael Heerdegen <michael_heerdegen@web.de>
Cc: help-gnu-emacs@gnu.org, Douglas Lewan <d.lewan2000@gmail.com>
Subject: Re: Compiling a recursive macro
Date: Sat, 13 Jun 2020 15:34:06 +0200 [thread overview]
Message-ID: <20200613153406.135158eb@jcubic> (raw)
In-Reply-To: <87h7vfi9cu.fsf@web.de>
[-- Attachment #1: Type: text/plain, Size: 2302 bytes --]
> BTW2, a small problem with your posted version is that it adds one `let'
> binding per branch, but only the outermost is needed:
Thanks I've fixed my macro now it looks like this:
(define-macro (case val . list)
"(case value
((<items>) result1)
((<items>) result2)
[else result3])
Macro for switch case statement. It test if value is any of the item. If
item match the value it will return coresponding result expression value.
If no value match and there is else it will return that result."
(let ((value (gensym)))
`(let ((,value ,val))
,(let iter ((list list))
(if (pair? list)
(let* ((item (car list))
(first (car item))
(result (cadr item))
(rest (cdr list)))
`(if (member ,value ',first)
,result
,(if (and (pair? rest)
(eq? (caar rest) 'else))
(cadar rest)
(if (not (null? rest))
(iter rest))))))))))
It's written in Scheme because that was my original implementation, I used
named let in Emacs you probably will need flet or create helper function
outside of macro. So I guess I will need to use that first implementation
only as example of recursive macro.
There is another example of recursive macros in book [Let Over Lambda][1] in
printed version on page 135. And there is also chapter in [On Lips][2] on page
139 where there is one problem that you may find (probably issue OP had) if
you expand with expansion that have `if` test inside, it will create infinite
loop, expansion need to be conditional so recursive check that stop the loop
need to be called at macro level, it can't be present in expansion.
This is example macro that will not work
(defmacro nthb (n lst)
`(if (= ,n 0)
(car ,lst)
(nthb (- ,n 1) (cdr ,lst))))
here recursive guard is present in expansion and it will not compile becuase
it's infinite loop.
[1]: https://letoverlambda.com/index.cl/guest/chap5.html#sec_5
[2]: http://www.paulgraham.com/onlisptext.html
--
Jakub Jankiewicz, Web Developer
https://jcubic.pl/me
[-- Attachment #2: Podpis cyfrowy OpenPGP --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
next prev parent reply other threads:[~2020-06-13 13:34 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-06-11 20:15 Compiling a recursive macro Douglas Lewan
2020-06-11 20:57 ` Stefan Monnier
2020-06-11 21:23 ` Douglas Lewan
2020-06-11 21:32 ` Michael Heerdegen
2020-06-11 21:38 ` Douglas Lewan
2020-06-11 21:55 ` Michael Heerdegen
2020-06-11 22:21 ` Douglas Lewan
2020-06-12 10:49 ` Michael Heerdegen
2020-06-12 16:11 ` Douglas Lewan
2020-06-12 2:10 ` Douglas Lewan
2020-06-13 9:49 ` Michael Heerdegen
2020-06-13 13:34 ` Jakub Jankiewicz [this message]
2020-06-11 22:02 ` Stefan Monnier
2020-06-11 21:27 ` Michael Heerdegen
2020-06-12 0:03 ` Jakub Jankiewicz
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/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200613153406.135158eb@jcubic \
--to=jcubic@onet.pl \
--cc=d.lewan2000@gmail.com \
--cc=help-gnu-emacs@gnu.org \
--cc=michael_heerdegen@web.de \
/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).