guile-devel
[Top][All Lists]
Advanced

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

Re: [PATCH] local-eval, local-compile, and the-environment (v3)


From: Mark H Weaver
Subject: Re: [PATCH] local-eval, local-compile, and the-environment (v3)
Date: Sun, 15 Jan 2012 14:52:35 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.92 (gnu/linux)

David Kastrup <address@hidden> writes:

>> You are suggesting that we wrap the expression within a (let () ...),
>> for the dubious benefit of allowing you to wrap the local forms in
>> (begin ...) instead of (let () ...).
>
> Are there even situations where you could put definitions after begin?
> How are they different from the situation where you can't?
>
>> I don't see any compelling benefit to this.  On the other hand, I see
>> less elegant semantics and potential confusion among users, who might
>> reasonably expect the definitions in their (begin ...) to be added to
>> the implicit `letrec', as would happen if the (begin ...) were put in
>> place of (the-environment).
>
> begin can start with definitions, but not always?  But (let () ...) can?
>
> Scheme is weird.

`begin' is indeed quite weird in Scheme.  Its meaning depends on the
context where it is found:

* In expression context, it is like `progn' in Lisp.  It evaluates the
  expressions in order from left to right, and returns the value(s) of
  the last expression.

* At the top-level, or within an internal body where local definitions
  are allowed (i.e. if every form above it in the internal body is a
  definition), it is a _splicing_ operator: it acts as if the contents
  of the begin were spliced into the surrounding context.  This is
  mainly for the benefit of macros, so that they may expand into
  multiple definitions.

For example:

  (let ((x 1))
    (define (get-x) x)
    (begin
      (define x 2)
      (get-x)))

is equivalent to:

  (let ((x 1))
    (define (get-x) x)
    (define x 2)
    (get-x))

which is equivalent to:

  (let ((x 1))
    (letrec ((get-x (lambda () x))
             (x 2))
      (get-x)))

and therefore the result is 2, because the reference to `x' within
(get-x) refers to the inner binding of `x'.

     Mark



reply via email to

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