[CM] __func__ in lisp?

Kjetil S. Matheussen k.s.matheussen at notam02.no
Thu Jul 23 08:40:41 PDT 2009



On Thu, 23 Jul 2009, Kjetil S. Matheussen wrote:

>
>
> On Thu, 23 Jul 2009, Bill Schottstaedt wrote:
>
>> I've googled around and looked through clTl2 but I can't find a way
>> in Lisp (scheme) to get the current function name (from an embedded
>> macro, for example).  Is there some (non-)standard way to do this?
>>
>
> At least in Guile, you can redefine 'define'.
> So using a global variable to store the last called
> function should work. Untested:
>
> (define last-called-function #f)
>
> (define old-define define)
>
> (define-macro (define def . code)
>   (if (pair? def)
>       (let ((name (car def)))
>         (set! code `((lambda ,(cdr def)
>                        (set! last-called-function ',name)
>                        (let ()
>                          , at code))))
>         (set! def name)))
>   `(old-define ,def
>                (let ();
>                  (set! last-called-function ',name)
>                  (let ()
>                    , at code))))
>

I'm not sure if I quite understood what you ment though, but
here's a better version.

(define-macro define
   (let ((old-define define))
     (lambda (def . code)
       (if (pair? def)
           (let ((name (car def)))
             (set! code `((lambda ,(cdr def)
                            , at code)))
             (set! def name)))
       `(,old-define ,def
            (let ((current-function-name ',def))
              , at code)))))

guile> (define (a) (display current-function-name)(newline))
guile> (a)
a
guile>



More information about the Cmdist mailing list