guile-user
[Top][All Lists]
Advanced

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

Re: Closure?


From: Neil Jerram
Subject: Re: Closure?
Date: Sun, 13 Jul 2008 23:56:48 +0100

2008/7/13 Maciek Godek <address@hidden>:
>> except that the last line fails with a "Bad define placement" error.
>> That's because there are special rules for defines inside lexical
>> scopes.
>
> As the practise shows, although guile documentation says something
> different. In section 3.1.4.7 (A Shared Persistent Variable)
>
> "An important detail here is that the `get-balance' and `deposit'
> variables must be set up by `define'ing them at top level and then
> `set!'ing their values inside the `let' body.  Using `define' within
> the `let' body would not work: this would create variable bindings
> within the local `let' environment that would not be accessible at top
> level."
>
> So one might conclude that it _is_ possible to use define inside
> a 'let' form.

Which would be correct!  For example:

(let ((a 1))
  (define b 2)
  (+ a b))
=>
3

Whereas:

(let ((a 1))
  (display a)
  (newline)
  (define b 2)
  (+ a b))
=>
ERROR: Bad define placement

The "special rules" are just that  any defines have to come before
anything else in the body of the let.

I don't know exactly how it works out that using a define in
local-eval falls foul of the define placement rule, but it is not hard
to imagine that it could do.

> Yes, since there's local-eval and the-environment, everything I've
> ever dreamed of is possible :)
> But as I've concluded from the discourse,  neither of these is
> defined in R5RS (and it makes me wonder)

Well I've never thought this through before, but perhaps that is
because in many cases it is equivalent to create a lambda at the point
where you would call the-environment, containing the code that you
would later pass to local-eval.

For example, the ++ example then becomes:

(define ++ (let ((c 0)) (lambda () (begin (set! c (+ c 1)) c))))

- which is the traditional way of writing this example.

Regards,
         Neil




reply via email to

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