make list 0 versus make list []
[1/5] from: princepawn::lycos::com at: 27-Aug-2000 11:30
I read something in the REBOL McGraw-Hill book, but would like to verify it.
Is there a difference on subsequent invocations of a function if I bind a (local?) value
using make list! 0 versus make list! []
So, now I have created 4 cases:
make list!.....0 or []
the word being defined...../local to function or global to rebol
If I could get an answer to all four of these cases, I would be a much clearer on state
retention between function invocations.
Thanks.
Terrence "Perl Refugee" Brannon
[2/5] from: al:bri:xtra at: 28-Aug-2000 15:20
Terrence "Perl Refugee" Brannon wrote:
> I read something in the REBOL McGraw-Hill book, but would like to verify
it.
> Is there a difference on subsequent invocations of a function if I bind a
(local?) value using make list! 0 versus make list! []
> So, now I have created 4 cases:
>
> make list!.....0 or []
> the word being defined...../local to function or global to rebol
>
> If I could get an answer to all four of these cases, I would be a much
clearer on state retention between function invocations.
With this:
make list! 999
the number 999 suggests to Rebol how much space to allocate to the list.
With this:
make list! []
the "[]" suggests to Rebol that no minimum size of memory be allocated
to the list!.
In both cases, Rebol will expand or contract the list as required to hold
the data in the list.
If you want to make the word local to the function, then:
MyListFunction: function [] [MyList] [
MyList: make list! 999
;...
]
If you want to make the word global:
MyListFunction: function [] [] [
MyList: make list! 999
;...
]
In both of the above functions, the 'MyList word is allocated a new 'list!
each time the function MyListFunction is invoked. If you want to keep the
same list, each time the function is invoked, put:
MyList: make list! 999
outside of MyListFunction. Better still, use a object!:
MyList: make object! [
MyList: make list! 999
MyOperation: function [] [] [
;...
]
]
Andrew Martin
ICQ: 26227169
http://members.xoom.com/AndrewMartin/
[3/5] from: agem:crosswinds at: 28-Aug-2000 17:48
maybe Terrence read something about [a: [] ] versus
[ a: copy [] ] versus [ a: make list! [] ] ? in the
last case the block is copied too, means you have
allways an empty list. with [a: []] the block will
remember its changes on subsequent invocations.
--- [Al--Bri--xtra--co--nz] wrote on 28-Aug-2000/15:20:44+12:00
[4/5] from: galtbarber:mailandnews at: 28-Aug-2000 14:05
I am probably behind you all on this thread, but
when I tried it I was surprised. Rebol may have
always been this way and I just didn't notice before?
Interesting!
>> make block! 0
== []
>> make list! 0
== make list! []
>> mold make list! [stuff and more stuff]
== "make list! [stuff and more stuff]"
>> mold make block! [stuff and more stuff]
== "[stuff and more stuff]"
So there is a fundamental difference
between them?
I had thought the list would behave like block itself.
When I see the list reported like that it reminds
me of make object! in the way that mold or probe
return it.
-Galt
[5/5] from: galtbarber:mailandnews at: 28-Aug-2000 14:36
> In both cases, Rebol will expand or contract the list as required to hold
> the data in the list.
It is true that Rebol will expand memory for a block
or list automatically, although you can help speed
it up by pre-initializing to a large number of
elements if you know they will be needed.
In the case of a block, however, I think it does
not release memory automatically. I think it holds
onto the largest memory that instance of the block
has used in case you need that space again.
So, it is quite possible that the same thing applies
to lists, as far as system storage usage goes.
Obviously, if you really knew that you had used tons
of memory and that you wouldn't need that again,
you could create a copy of the original that would
be no larger than needed.
b: make block! 10000000
[... do some stuff, now it is just small, but non-empty...]
b: make block! b
now b will point to a new smaller block, I hope,
and the big original should be garbage recycled.
of course if that didn't work, this should work:
b: copy b
Anyway, I am not sure how to check the exact amount of ram
used accurately. Recycle seems to help sometimes, but
I can't always get the memory reported by taskmgr to
show it's minimum value, so Rebol must be hanging onto
some ram in case the user asks for more later.
Can someone out there enlighten us?
Thanks,
-Galt
p.s. for most of my programs, I don't bother to
pre-initialize the size. I think that's not necessary
unless you have a specific small bit of code that
must be highly optimized for speed, which is usually
not the case for my own programs.