guile-user
[Top][All Lists]
Advanced

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

Re: defined?


From: Marius Vollmer
Subject: Re: defined?
Date: 13 Jan 2003 16:24:40 +0100
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2

Matt Hellige <address@hidden> writes:

> [Egil Moeller <address@hidden>]
> > The problem is
> > 
> > guile> (let ((b 5)) (eval 'b  (interaction-environment)))
> > <unnamed port>: In expression b:
> > <unnamed port>: Unbound variable: b
> > ABORT: (unbound-variable)
> 
> Yes, good point. I suppose the problem is that you want a closure
> representing the actual current environment rather than the top-level.
> It seems like there should be a way to do this...?

I think we should not support run-time lookup in lexical environments.
Doing so would make the job of a compiler much harder.  And I think it
is generally very unclean way of specifying and implementing the
environment that foreign code is executed in.

Say you have a function 'integrate' that implements a method for
numerically integrating a function of one argument.  Your approach,
using local variables, might look like:

    ;; Integrate F from LOWER to UPPER.  F is a Scheme expression,
    ;; represented as a s-exp, where 'x' is the free variable.  For
    ;; example:
    ;;
    ;;   (integrate-source '(* x x) 0 1)

    (define (integrate-source f lower upper)
      (define (eval-f arg)
        (let ((x arg))
          (local-eval f (the-environment))))
      ... use eval-f to get values of f.)

The standard Scheme method, however, is not to bother with any
evaluation of foreign code, but to pass 'f' as a closure:

    ;; Integrate F from LOWER to UPPER.  F is a procedure of one argument.
    ;; For example:
    ;;
    ;;   (integrate (lambda (x) (* x x)) 0 1)

    (define (integrate f lower upper)
      ... use f directly to get values of f.)

If you want to allow the function to be specified in source form, use
eval just once to create a closure:

    ;; Integrate F from LOWER to UPPER.  F is a Scheme expression,
    ;; represented as a s-exp, where 'x' is the free variable.  For
    ;; example:
    ;;
    ;;   (integrate-source '(* x x) 0 1)

    (define (integrate-source f lower upper)
      (integrate (eval `(lambda (x) ,@f) (interaction-environment))
                 lower upper))

My point is that it is much cleaner to avoid local-eval and that you
should avoid it because of that.  Scheme has other, much nicer
features that you can use to specify the and implement the environment
that foreign code is executed in.

-- 
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3  331E FAF8 226A D5D4 E405




reply via email to

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