From: Oliver Scholz <alkibiades@gmx.de>
Subject: Re: 'let' for functions ?
Date: Sat, 26 Apr 2003 22:01:51 +0200 [thread overview]
Message-ID: <uwuhh3u4w.fsf@ID-87814.user.dfncis.de> (raw)
In-Reply-To: 87ptn912ya.fsf@cs.uga.edu
Ed L Cashin <ecashin@uga.edu> writes:
[...]
> As an exercise I tried in vain to create a simple implementation of
> labels that wouldn't require loading cl. I couldn't do it, though,
> and had to get on with what I was supposed to be doing. ;)
You need not fear the cl package. If you put
`(eval-when-compile (require 'cl)' into your code, the compiled
package won't require cl.
However, since this is, as you say, a nice exercise, I give it a
shot. (Although I am supposed to be doing something different, too
...)
> I'd be very interested, though, to see such a simple implementation of
> labels if there's an elisp guru who would enjoy trying to find it.
> The hard part for me was recursion support.
I am not a Lisp-Guru, but here is my version anyways. I'd like to hear
whether there is any problem with this approach:
(defmacro my-simple-labels (spec &rest body)
;; We `fset' the symbols temporarily and restore the original state
;; after the body form was executed.
(let ((symbol (make-symbol "symbol"))
(definition (make-symbol "definition"))
(sfunc (make-symbol "sfunc")))
`(progn
(let (,symbol)
(dolist (,definition ',spec)
(setq ,symbol (car ,definition))
;; Store function definition, if necessary.
(when (fboundp ,symbol)
(put ,symbol 'my-simple-labels (symbol-function ,symbol)))
;; New function definition.
(fset ,symbol (cons 'lambda (cdr ,definition)))))
(unwind-protect (progn ,@body)
;; Clean-up
(let (,sfunc)
(dolist (,symbol ',(mapcar 'car spec))
(setq ,sfunc (get ,symbol 'my-simple-labels))
(if ,sfunc
(fset ,symbol ,sfunc)
(fmakunbound ,symbol))))))))
Oliver
--
7 Floréal an 211 de la Révolution
Liberté, Egalité, Fraternité!
next prev parent reply other threads:[~2003-04-26 20:01 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <1036073066.48586.0@iris.uk.clara.net>
2002-10-31 14:18 ` 'let' for functions ? Hannu Koivisto
2003-04-26 19:19 ` Ed L Cashin
2003-04-26 20:01 ` Oliver Scholz [this message]
2003-04-26 20:20 ` Ed L Cashin
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=uwuhh3u4w.fsf@ID-87814.user.dfncis.de \
--to=alkibiades@gmx.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).