guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Guile Introspection


From: Marco Maggi
Subject: Re: Guile Introspection
Date: Mon, 9 Jul 2007 08:28:46 +0200

Ciao,

"Mike Gran" wrote:
>Or, what is the scheme version of the following C code
>
>printf("%s %d\n", __FILE__, __LINE__);

I like a solution that redefines DEFINE without introducing
an environment in the body of the function. To do it: you
have to take the body in question and treat it as a tree,
mapping selected symbols to the values you want.

If I am correct in assuming that you are a Scheme beginner
and the following code is no clear enough, just ask.

;; ----------------------------------------

(define (map-tree func tree)
  (map (lambda (v)
         (if (list? v)
             (map-tree func v)
           (func v))) tree))

(define (subst-tree old new tree)
  (map-tree (lambda (v)
              (if (eq? old v) new v)) tree))

(define saved-define define)

;; ----------------------------------------

(define *current-file-name* "proof.scm")

(define-macro (define name-and-args . body)
  (let* ((fname (symbol->string (car name-and-args)))
         (args  (cdr name-and-args))
         (body1 (subst-tree
                 '*current-file-name* *current-file-name*
                 (subst-tree '*current-function-name* fname body))))
    `(saved-define ,name-and-args ,@body1)))

;; ----------------------------------------
;; tests

(define (my-func)
  (format #t "file: ~A, func: ~A~%"
          *current-file-name*
          *current-function-name*))

(define (other-func alpha beta)
  (format #t "file: ~A, func: ~A, a ~A, b ~A~%"
          *current-file-name*
          *current-function-name*
          alpha beta))

(define (further-func . args)
  (apply format #t "file: ~A, func: ~A, a ~A, b ~A~%"
         *current-file-name*
         *current-function-name*
         args))

(my-func)
(other-func 123 'abc)
(further-func 123 'abc)

--
Marco Maggi

"They say jump!, you say how high?"
Rage Against the Machine - "Bullet in the Head"





reply via email to

[Prev in Thread] Current Thread [Next in Thread]