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!
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__
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!
Yes. See:
http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-8.html
--
__Pascal Bourguignon__
>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
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.
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!