: [] in a function => was {Re: Re: to-char}
[1/6] from: jason:cunliffe:verizon at: 13-Feb-2002 20:44
From: "Carl Read" <[carl--cybercraft--co--nz]>
> > What is going on when I call it within a function and then call the
> > function repeatedly [when the previous values remain]. ie: why do I
> > _really_ need clear or copy?
>
> The reason can be seen in the above, in that it remains the same
> block. Meaning you haven't made a copy of the block. Continuing on
> with the above, this shows the difference 'copy makes...
Carl..thanks
ok. This is what I still don't deeply get:
>> someblock: []
== []
>> append someblock "a"
== ["a"]
>> append someblock "b"
== ["a" "b"]
>> append someblock "c"
== ["a" "b" "c"]
>> someblock: []
== []
>> someblock
== []
>> head someblock
== []
BUT.. when I do the same thing within a script function:
change-block: func [str [string!] /local someblock][
someblock: []
append someblock str
return someblock
]
>> change-block "hello"
== ["hello"]
>> change-block "goodbye"
== ["hello" "goodbye"]
Q: Why, at the console does someblock: [] create a fresh block, but within a
script function someblock: [] persists and needs to be remedied by
copy [], clear [] or make block! 123 statements?
> More on functions: A series is created when the function is first
> created and not each time the function is called, which is why what's
> in a series will persist from function-call to function-call unless
> you specifically clear it or make a copy of it. Whether to use 'copy
> or 'clear (or neither for that matter) will depend on the behaviour
> you want from the series.
aha!..er..hmm, but this sounds like the second time I call my change-block
function, it is sees 'someblock: []' rebol says "oh I already got that", yet
when it reads copy, clear make it changes its mind and redfines someblock?
This seems pretty inconsistent to me. I know how to use it, but want to grok
further rebolzen. Please keep trying to explain me this rudiment..
thanks again
./Jason
[2/6] from: al:bri:xtra at: 14-Feb-2002 17:43
Jason wrote:
> This is what I still don't deeply get:
> Q: Why, at the console does someblock: [] create a fresh block, but within
a script function someblock: [] persists and needs to be remedied by copy
[], clear [] or make block! 123 statements?
When you type this at the console:
someblock: []
you're assigning the value of the word 'someblock to the next value
following, which happens to be a block!. When you type in the superficially
similar line at the console again:
someblock: []
Note that the "[]" is _another_ block value, not the same block. As can
be seen:
>> b: someblock: []
== []
>> append someblock "a"
== ["a"]
>> append someblock "b"
== ["a" "b"]
>> append someblock "c"
== ["a" "b" "c"]
>> someblock: []
== []
>> b
== ["a" "b" "c"]
Now 'b and 'someblock refer to different block! values.
Here's a function body:
>> Body: [
[ someblock: []
[ append someblock random 10
[ someblock
[ ]
== [
someblock: []
append someblock random 10
someblock
]
Now we 'do Body, much like a function, twice:
>> do Body
== [5]
>> probe Body
[
someblock: [5]
append someblock random 10
someblock
]
== [
someblock: [5]
append someblock random 10
someblock
]
>> do Body
== [5 2]
>> probe body
[
someblock: [5 2]
append someblock random 10
someblock
]
== [
someblock: [5 2]
append someblock random 10
someblock
]
And inspect the second value in the block! value refered to by 'Body:
>> probe pick Body 2
[5 2]
== [5 2]
I hope that helps!
Andrew Martin
ICQ: 26227169 http://valley.150m.com/
[3/6] from: al:bri:xtra at: 14-Feb-2002 17:48
And here's how to get similar behaviour with a non-series value:
>> body: [
[ Number: 123
[ body/2: random 10
[ probe body
[ body
[ ]
== [
Number: 123
body/2: random 10
probe body
body
]
>> do body
[
Number: 3
body/2: random 10
probe body
body
]
== [
Number: 3
body/2: random 10
probe body
body
]
>> do body
[
Number: 2
body/2: random 10
probe body
body
]
== [
Number: 2
body/2: random 10
probe body
body
]
Andrew Martin
ICQ: 26227169 http://valley.150m.com/
[4/6] from: brett::codeconscious::com at: 14-Feb-2002 17:18
Hi Jason,
Here's an "article" I recently wrote that might help:
http://www.codeconscious.com/rebol/articles/rebol-concepts.html
Warning though, it has not been critiqued in any way.
> Q: Why, at the console does someblock: [] create a fresh block, but within
a
> script function someblock: [] persists and needs to be remedied by
> copy [], clear [] or make block! 123 statements?
My answer would be that each time you enter a [] at the console, Rebol will
recognise the string form of a block and create one in memory - so you get
multiple - one each time you do it. Within a script however, Rebol will
recognise the form of the block once (when the script is loaded) and thus
create the block in memory once. I don't know if this is *exactly* right but
it seems like it could be close enough.
Regards,
Brett.
[5/6] from: carl:cybercraft at: 14-Feb-2002 20:14
On 14-Feb-02, Jason Cunliffe wrote:
> From: "Carl Read" <[carl--cybercraft--co--nz]>
>> > What is going on when I call it within a function and then call
<<quoted lines omitted: 42>>
> already got that", yet when it reads copy, clear make it changes its
> mind and redfines someblock?
I don't have a deep understanding of how functions work, but I'd guess
that when functions are created space is set aside for blocks and
strings and the like so that when the function is called a line like
this...
a-block: []
is really something like...
a-block: reference-to-a-block
which would just give 'a-block the block's reference, whereas...
a-block: clear reference-to-a-block
would clear the block of its contents before giving 'a-block its
reference and...
a-block copy reference-to-a-block
would make a copy of the block and then give 'a-block the reference to
the new block. (The old block would be discarded unless other words
were also referencing it.)
> This seems pretty inconsistent to me. I know how to use it, but want
> to grok further rebolzen. Please keep trying to explain me this
> rudiment..
Well, I hope the above helps, and don't worry, you're certainly not
the first to wonder about this. I think it's good, as it allows for
more flexible functions, and you can still have them behave in a
hard-wired fashion if you wish, albeit with the need for a little
more thought about what you're doing. What's more, you do start to
think of it as being consistant after a while. Honest! (:
--
Carl Read
[6/6] from: lmecir:mbox:vol:cz at: 14-Feb-2002 11:14
Q: Why, at the console does someblock: [] create a fresh block, but
> within a script function someblock: [] persists and needs to be
> remedied by copy [], clear [] or make block! 123 statements?
I think, that this really needs an explanation. The simplest answer to this
is, that the Rebol interpreter doesn't create a fresh block when it
evaluates the expression:
someblock: []
How come, that we get two blocks when we type in the following console
expressions?
; Expression # 1:
someblock-1: []
; Expression # 2:
someblock-2: []
; Probing the sameness:
same? someblock-1 someblock-2 ; == false
The answer is, that the Rebol interpreter does more than evaluating
expressions. Its first job is to create a Rebol value that it can evaluate.
The normal order of the interpretation looks as follows:
; the interpreter reads the input, which can be simulated as follows
input-line-1: "someblock-1: []"
; the interpreter transforms the input to a block
loaded-line-1: load input-line-1
; this was when the empty block got created:
type? second loaded-line-1 ; == block!
; now (finally) the expression gets evaluated like
do loaded-line-1
; we can easily make sure, that no block has been created during the
evaluation of the expression:
someblock-2: someblock-1
; let's evaluate the expression once more:
do loaded-line-1
same? someblock-1 someblock-2 ; == true
If we compare this to any "paradoxical" cases, we will find out, that the
behaviour is the same, and the behaviour looks "inconsistent" just because a
block is evaluated more than once, while being created just once.
Does this shed some light on the subject?
Regards
Ladislav
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted