This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

dynamic scope


Hi,

"load" works globally even if it was called in a procedure:

szi@olivia:~/scheme$ cat dynamic-load.scm
(define a 1)
(define (dynamic)
  (load "dynamic-load-define.scm")
  a)
(display (dynamic)) (newline)
(display a) (newline)
szi@olivia:~/scheme$ cat dynamic-load-define.scm 
(define a 2)
szi@olivia:~/scheme$ guile -s dynamic-load.scm
2
2

R4RS does not define that this is necessary. I think it would be better
to have a local "load". And together with this a local
"load-from-string" or "load-from-list". This would make configuration
tasks really easy.

Example: You have a Scheme tree with HTML tags like this one:

(define example-mini-html-document
  '(html
    (head )
    (body "moin")))

With a dynamic load you can write transformation filters without parsing
the HTML code by simply defining a function for every HTML tag without
defining the HTML functions in the translator function. This would make
transformations much faster, because it is not necessary to navigate
through the tree. 

Instead of this:

(define $ string-append)
(define (translate-to-html sml-document)
  (letrec ((<> (lambda (name) ($ "<" name ">")))
           (<>nl (lambda (name) ($ "<" name ">\n")))
           (</> (lambda (name) ($ "<" name ">")))
           (</>nl (lambda (name) ($ "<" name ">\n")))
           (html (lambda (. args) ($ (<>nl "HTML") args (</>nl
"HTML"))))
           (head (lambda (. args) ($ (<>nl "HEAD") args (</>nl
"HEAD"))))
           (body (lambda (. args) ($ (<>nl "BODY") args (</>nl
"BODY"))))
           )
    (if (sml:html? sml-document)
        (catch #t
               (lambda () (eval sml-document))
               (lambda (key . args)
                 (display key)
                 (display args)
                 (newline)
                 (backtrace)
                 'error-during-eval))
        'no-html)))

I would like to be able to write something like this:

(define (translate to-html sml-document)
  (if (sml:html? sml-document)
      (catch #t
	     (lambda ()
	       (load-from-list to-html)    ; <--
	       (eval sml-document))
	     (lambda (key . args)
	       (display key)
	       (display args)
	       (newline)
	       (backtrace)
	       'error-during-eval))
      'no-html))

-- http://www.ping.de/sites/aibon/