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]

Re: Here's a guile profiling tool/a question/a *remarkable* guile fact (was Re: Guile profiling tools?)


csk@cs.washington.edu (Craig Kaplan) writes:

 > > defines inside of defines are *not* just being evaluated at load
 > > time!!!!!  I find this truly remarkable!  Why is this!?!?!?!?
 > 
 > Isn't it just because the inner defines can reference objects in the
 > scope of the outer defines?  Example:
 > 
 > 	(define (f x y)
 > 		(define r1 (* x y))
 > 		(define r2 (/ x y))
 > 		(list r1 r2))
 > 
 > 	guile> (f 10 6)
 > 	(60 1.66666666666667)
 > 	guile> (f 99 33)
 > 	(3267 3)
 > 
 > I need to re-evaluate the defines for r1 and r2 every time I evaluate f, 
 > because their values change.

I guess so...

But, R4RS states that nested defines should be equivalent to letrec,
and that each expression assigned by the letrec must evaluatable
"without assigning or referring to the value of any <variable>".

In particular, the following is illegal, but both guile & STk allow
it:
   guile> (define (f x y)
	     (define r1 (* x y))
	     (define r2 (* 2 r1))
	     (list r1 r2))
   guile> (f 10 6)
   (60 120)
   guile>  (f 99 33)
   (3267 6534)

whereas guile raises an error condition for the equivalent letrec:

   guile> (define (f x y)
             (letrec ((r1 (* x y))
	              (r2 (* 2 r1)))
	         (list r1 r2)))
   guile> (f 10 6)
   ERROR: While evaluating arguments to * in expression (* 2 r1):
   ERROR: Unbound variable: r1
   ABORT: (misc-error)

   Type "(backtrace)" to get more information.
   guile>

So, something's a *little* fishy, although it doesn't really violate R4RS.

I guess I'm going to have to stop using local variables for such
things...

-- 
Harvey J. Stein
BFM Financial Research
hjstein@bfr.co.il