Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Different evaluation of nested define and "global" define

22 views
Skip to first unread message

Nando

unread,
Nov 23, 2009, 6:12:33 PM11/23/09
to
Hy.
I know this is not a correct (or good) way of doing this, but I was
just trying it out. Anyway I do not understand why these two codes
give diferent results.

If anyone could offer some advice, it would be most helpfull!

A
>(define money 100)
>(define money (- money 10))
>money
90


B
>(define (withdrawal x)
(if (> money x)
(with-aux x)
"you need more money"))

>(define (with-aux x)
(define money (- money x))
money)
>(withdrawal 10)
-: expects type <number> as 1st argument, given: #<undefined>; other
arguments were: 25

(this minus is the one in "with aux").

Thanks!

Pascal J. Bourguignon

unread,
Nov 23, 2009, 6:39:02 PM11/23/09
to
Nando <nando.p...@gmail.com> writes:

This is because local define is local. It is equivalent to:

(define (with-aux x)
(let ((money '#<undefined>))
(set! money (- money x))
money))

that is, substiting z for money:

(define (with-aux x)
(let ((z '#<undefined>))
(set! z (- z x))
z))

My advice would be to avoid local define, and to always use let
instead.

--
__Pascal Bourguignon__

Nando

unread,
Nov 24, 2009, 5:00:59 PM11/24/09
to
On 23 Nov, 23:39, p...@informatimago.com (Pascal J. Bourguignon)
wrote:

Hy Pascal!

Thanks for your help!
But why dosen't that happens here:


>(define money 100)
>(define money (- money 10))
>money
90

Is define evaluated differently in global environement?
My idea is it would always be the same. So here it would go:

>(define money 100)
>(define money '#<undefined>) ; this would be sort of mixed with the next instruction...


>(define money (- money 10))
>money

error...

Do you see what I mean?....

Thanks again!

Pascal J. Bourguignon

unread,
Nov 24, 2009, 5:38:33 PM11/24/09
to
Nando <nando.p...@gmail.com> writes:
> Is define evaluated differently in global environement?

Yes. See:
http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-8.html
--
__Pascal Bourguignon__

Aaron W. Hsu

unread,
Nov 24, 2009, 8:11:15 PM11/24/09
to
Nando <nando.p...@gmail.com> writes:

>On 23 Nov, 23:39, p...@informatimago.com (Pascal J. Bourguignon)
>wrote:

>> My advice would be to avoid local define, and to always use let
>> instead.

Internal definitions are useful for defining internal procedures, but if
you are looking to define things like variables that will contain
numbers, 'let' makes more sense. 'define' is overspecified for things
that don't require LETREC or LETREC* type semantics, but they certainly
make those things that require recursive bindings more convenient.

>But why dosen't that happens here:
>>(define money 100)
>>(define money (- money 10))
>>money
>90

The REPL is not like other interaction environments. In general, at the
REPL top-level, you are able to rebind values in the same "scope" which
is what is happening here. If you tried to wrap this in a LET form, or
if you tried to do this in a top-level R6RS program, you would get an
error that you had tried to define 'money' multiple times. SET! changes
a names binding, whereas DEFINE is for binding new values. At the REPL
top-level, they are often the same thing, depending on the Scheme
implementations REPL semantics. This is for convenience.

>Is define evaluated differently in global environement?

They are different.

Aaron W. Hsu

jos koot

unread,
Nov 26, 2009, 5:33:40 AM11/26/09
to
On Nov 24, 12:12 am, Nando <nando.portu...@gmail.com> wrote:
> Hy.
> I know this is not a correct (or good) way of doing this, but I was
> just trying it out. Anyway I do not understand why these two codes
> give diferent results.
>
> If anyone could offer some advice, it would be most helpfull!
>
> A>(define money 100)
> >(define money (- money 10))

The second define is like (set! money (- mody 10))
Now set! and define are quite different.
(define var value)
The value is evaluated in an environment including tjhe new binding of
var.
(set! var value)
The value is computed. If it refers to the var, the old value is used.


> >money
>
> 90
>
> B>(define (withdrawal x)
>
>   (if (> money x)
>       (with-aux x)
>       "you need more money"))
>
> >(define (with-aux x)
>
>   (define money (- money x))

Here first a new binding is made for money. The old binding is
shadowed already before (- money x) is evaluated.

Nando

unread,
Dec 8, 2009, 6:46:05 PM12/8/09
to

Thanks agina Pascal, Aaron and jos!
It took me some time to answer because I was away!

You were much more helpful than my teacher!!!

Thanks again!

0 new messages