Subject: Re: Define in a define
From: rpw3@rigden.engr.sgi.com (Rob Warnock)
Date: 1999/11/24
Newsgroups: comp.lang.scheme
Message-ID: <81fodo$3t0c6@fido.engr.sgi.com>
David Delahaye  <David.Delahaye@inria.fr> wrote:
+---------------
| Wolfgang Hukriede wrote:
| > Anyway, "begin" does not introduce scope - neither does "define" - so
| > "g" is bound globally in any case. So, what were you trying to achieve
| > when you said (define f (define (g) '()) g) ??
| 
|     Indeed, my example is too irrelevant. May be with a real example, it
| will be clearer. You have a function called do-quick-sort which takes
| two predicates equal and less, and "returns" a function which makes a
| quick-sort according to equal and less. To define a quick-sort on a
| structure S, I must write:
| 
| (define quick-sort-S
|   (let ()
|     (define equal ...)
|     (define less ...)
|     (do-quick-sort equal less)))
| 
|     I don't want equal and less to be known outside of quick-sort-S.
+---------------

They won't be; the "let" opens a new scope. But the above code is
expanded into:

  (define quick-sort-S
    (let ()
      (letrec ((equal ...)
	       (less ...))
        (do-quick-sort equal less))))

and you don't really need the mutual-recursion of a "letrec" in
this case, so a slightly-lower-overhead way of writing the above is:

  (define quick-sort-S
    (let ((equal (lambda (x y) ...))
	  (less (lambda (x y) ...)))
      (do-quick-sort equal less)))

or even simply:

  (define quick-sort-S
    (do-quick-sort
      (lambda (x y) ...)	; "equal" predicate
      (lambda (x y) ...)))	; "less" predicate

+---------------
| I think this is disappointing because I'd like to write:
| 
| (define quick-sort-S
|   (define equal ...)
|   (define less ...)
|   (do-quick-sort equal less))
+---------------

Why? What's wrong with a single simple "let"?  [middle example]


-Rob

-----
Rob Warnock, 8L-846		rpw3@sgi.com
Applied Networking		http://reality.sgi.com/rpw3/
Silicon Graphics, Inc.		Phone: 650-933-1673
1600 Amphitheatre Pkwy.		FAX: 650-933-0511
Mountain View, CA  94043	PP-ASEL-IA