[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: 'let' for functions ?
From: |
Oliver Scholz |
Subject: |
Re: 'let' for functions ? |
Date: |
Sat, 26 Apr 2003 22:01:51 +0200 |
User-agent: |
Gnus/5.090019 (Oort Gnus v0.19) Emacs/21.3.50 (windows-nt) |
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é!