guile-user
[Top][All Lists]
Advanced

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

Re: Binding symbols in stages


From: Hans Aberg
Subject: Re: Binding symbols in stages
Date: Sat, 6 Mar 2010 13:08:24 +0100

On 6 Mar 2010, at 12:13, Andy Wingo wrote:

 (define add1 (lambda (y) (+ x y)))
and then
 (define add (lambda (x) ...))
Thus in two separate steps, where the unbound symbol x in the first
expression is bound only in the second.

If I understand you correctly, this is not possible in normal Scheme.

I suspected that; thanks for the confirmation.

(I
say normal Scheme because you can build your own interpreter for another
kind of Scheme within Guile, one with dynamic binding, say.)

What do you have in your mind here?

The way to leave a bound identifier unbound is precisely to wrap it in a
closure. For example to "delay binding" of Y and Z in:

 (lambda (x) (+ x y z))

you need wrap the expression in a lambda:

 (lambda (y z)
   (lambda (x) (+ x y z)))

Which seems to be what you're trying to get away from, though I don't
know why.

I'm Scheme expressions as a part of the interface. So x >>= f constructs (lambda (x) f). Now, if I want to create a function, I can do that by
  symbol<integer> x("x");
  function1<integer, integer> inc = x >>= x + integer(2);
One option is to let the ">>=" operator evaluate the expression, and hand that over to "inc", which then will hold a procedure.

But then there is a problem when doing a curried function:
  symbol<integer> x("x"), y("y");
function2<integer, integer, integer> add = x >>= y >>= x + y; // Binds to the right. Then if y >>= x + y evaluates (lambda (y) (+ x y)), one ends up on the question I asked for: all attempts to use the symbol x in it will produce an unbound variable error.

So suppose I just store the lambda expression produced by ">>=" in the function class. Then scm_call_1() etc only acts on procedures. So I have to evaluate f(x)
  scm_primitive_eval(scm_list_2(f, x);

One drawback of this approach, I gather, is that the lambda expression isn't evaluated into a procedure, which might be slow.

So yet another approach might be to let the function class evaluate the expression into a procedure, and when a member f needs to be used in an expression, restore it as such by (lambda x' (f x')), where x' is an uninterned symbol.

  Hans






reply via email to

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