From: John L. Stein
Subject: Nested Defs
Date: 
Message-ID: <55r3op$1e74@uni.library.ucla.edu>
I need to make 

(define (make-it! s) 
	(define s 7))

work but it won't.

I can't use set! in the second line 
because I really need something like

(define (make-it! s) 
	(define s structB))

Where structB is some complex structure.

I need to make a bunch of structBs
so I need s to be a variable.

Any suggestions ?

Thanks
-jd

From: Clifford Beshers
Subject: Re: Nested Defs
Date: 
Message-ID: <yl9vibiesll.fsf@dynamo.cs.columbia.edu>
"John L. Stein" <·····@seas.ucla.edu> writes:

> 
> 
> I need to make 
> 
> (define (make-it! s) 
> 	(define s 7))
> 
> work but it won't.
> 
> I can't use set! in the second line 
> because I really need something like
> 
> (define (make-it! s) 
> 	(define s structB))
> 
> Where structB is some complex structure.
> 
> I need to make a bunch of structBs
> so I need s to be a variable.
> 
> Any suggestions ?
> 
> Thanks
> -jd
> 

You need eval, which is non-standard.  See the FAQ.

-- 
Clifford Beshers                     Computer Graphics and User Interfaces Lab
·······@cs.columbia.edu                         Department of Computer Science
http://www.cs.columbia.edu/~beshers                        Columbia University
From: Bert Van Vreckem
Subject: Re: Nested Defs
Date: 
Message-ID: <56cd2b$2ks@rc1.vub.ac.be>
John L. Stein (·····@seas.ucla.edu) wrote:

: I need to make 

: (define (make-it! s) 
: 	(define s 7))

: work but it won't.

: I can't use set! in the second line 
: because I really need something like

: (define (make-it! s) 
: 	(define s structB))

: Where structB is some complex structure.

Yes you can!!!
=>(define s 8)
s
=>s
8
=>(set! s (lambda (n) (* n 2)))
<#unspecified>
=>(s 8)
16
=>_

This is allowed.

Why you code doesn't work:
I hope you've heard from the environment model. No? Well, the environment
is a set of frames; frames are sets of bindings between the names of
variables and their values. When you evaluate a procedure, e.g.

=>(define a 2)
a
=>(define b 3)
b
=>(define (foo m n)
    (+ (* n n) (* m m)))
foo
=>(foo a b)
13
=>_

the interpreter extends the global environment (all the variables defined
at top-level) with a frame. In that frame come the bindings between the 
formal parameters (here: m and n) with the actual parameters (2 and 3,
the values of a and b).
The body of the function is evaluated in this frame, i.e. the code of the
body can 'see' the variables m, n and those defined at higher levels.
When you manipulate the variables m and n, you don't touch a and b.
I'll try to make some kind of picture of the situation:

 +---Global environment--------------------------------+
 | a    : 2                                            |
 | b    : 3                                            |
 | foo  : (lambda (..) ...)                            |
 | .....                                               |
 +-----------------------------------------------------+
           ^
           |
     +-(foo a b)-------+
     | m   : 2         |
     | n   : 3         |
     +-----------------+

You see? m and n are _copies_ of a and b with the same values.

Why my first example does work:
In Scheme, values have a type. This means that when you look at a certain
position in the memory, you can see what type this variable is. In
languages like C, the _pointers_ have a type. You can't see in the memory
what type that value is. So, it's not necessary to specify what type the
variable you define has...

Hope this helps, but if you still have problems, feel free to mail me.
I see you have some other problems, but I don't have time to read them all
now...

Seeya,\

Bert


--
+----------------------------------------------------------------+
|           Bert `Ernie' Van Vreckem <·······@is1.vub.ac.be>     |
| _    _              Free University of Brussels                |
|/ `--'/     Ninoofsesteenweg 70 - B-1760 Roosdaal (Belgium)     |
|  |=|==========##         Phone: +32 54 33 45 34                |
|\_,--\                      'Dormio Ergo Sum'                   |
+----------------------------------------------------------------+