Re: Use of PICK and ROLL



On Mar 15, 3:20 pm, "J Thomas" <jethom...@xxxxxxxxx> wrote:
On Mar 15, 9:40 am, "Alex McDonald" <b...@xxxxxxxxxxx> wrote:



On Mar 15, 3:44 am, "Ed" <nos...@xxxxxxxxxxx> wrote:

"Marcel Hendrix" <m...@xxxxxx> wrote in messagenews:12303412203560@xxxxxxxxxxxxxxxxx
Andrew Haley <andre...@xxxxxxxxxxxxxxxxxxxxxxx> writes Re: Use of PICK and ROLL

duke <dnorman...@xxxxxxxxxxxxxxxxxxxxx> wrote:
On Mar 11, 10:20 am, a...@xxxxxxxxxxxxxxxxxxxxxxxxxx (Anton Ertl)

I see the issue now! However could you address Leo's comment that
the stack should not be used as an array. How does this figure into
the use of PICK and ROLL?

A stack is, by definition, a data structure that is only accessed at
one end. An array, in contrast, may be accessed at any point: you can
ask for the third element of an array just as easily as asking for the
first. PICK and ROLL are therefore array operations, not stack
operations, because they can access any element of the stack.

I have always wondered about the famous quote "the stack should not be
used as an array." Up to now I was thinking that the author wanted to
say that the stack should not be abused as an array. But of course, what
he meant was that an array should not be abused as a stack. It is obviously
very bad and inefficient to cast array operations as stack ops.
...

For an example of "arrays on the stack" one need look no further
than GET-ORDER SET-ORDER

Apparently it's "ok" for ALSO PREVIOUS et al to juggle array
items put on the stack by GET-ORDER however it's deemed
"bad form" to use PICK / ROLL . Very amusing.

Dealing with "arrays on the stack" one needs primitives which
can be factored from GET-ORDER SET-ORDER :

S ( a-addr u -- i*x ) \ place u cells from a-addr to the stack

S> ( i*x a-addr u -- ) \ remove u cells from the stack to a-addr

So perhaps arrays on the stack and PICK / ROLL have a place
after all and we don't have to be scared of them :)

Except that ALSO PREVIOUS ONLY DEFINITIONS don't work on the stack (at
least, not in my copy of ANS94). So there's no role for ROLL or PICK.
Section A.16.6.2.0715 of the standard contains an implementation of
the search order words, and ROLL and PICK aren't mentioned at all; in
fact, I'd be hard pressed to suggest how to implement them using ROLL
and PICK, unless you replaced all the OVERs with 1 PICK I suppose.

GET-ORDER and SET-ORDER don't juggle items on the stack either, so I'm
confused by your assertions.

I thuoght the point was that if you needed to change the search order
in some drastic way, you might be tempted to use ROLL . Say you have 8
items in the search order and you know that you now need the 7th one
to be searched first. Or even worse, you need to remove the 7th from
the top from the search order.

It might usually be easier to just construct the search order you
want. But whenever the system gives you a long sequence of items on
the stack there's the chance you'll want to do something with the deep
ones. Probably not with SAVE-INPUT . Probably not with GET-ORDER but
maybe. Probably not with deeply-nested control structures, though CS-
PICK and CS-ROLL are what you'll use if you do. Since it's rare, it
was easier to define 2 routines tto do everything than CS-DUP CS-SWAP
CS-OVER CS-ROT etc.

Probably you won't need to manipulate the 7th item in the search
order. But if you do, PICK and ROLL make it easy.

I'd suggest that any code that needs to manipulate the search order to
this degree is seriously out of control. "Probably not" just isn't
strong enough; don't do it!

--
Regards
Alex McDonald

.



Relevant Pages

  • Re: Use of PICK and ROLL
    ... An array, in contrast, may be accessed at any point: ... PICK and ROLL are therefore array operations, not stack ... I thuoght the point was that if you needed to change the search order ...
    (comp.lang.forth)
  • Re: Use of PICK and ROLL
    ... An array, in contrast, may be accessed at any point: ... PICK and ROLL are therefore array operations, not stack ... can be factored from GET-ORDER SET-ORDER: ...
    (comp.lang.forth)
  • Re: Use of PICK and ROLL
    ... An array, in contrast, may be accessed at any point: ... PICK and ROLL are therefore array operations, not stack ... can be factored from GET-ORDER SET-ORDER: ...
    (comp.lang.forth)
  • Re: Use of PICK and ROLL
    ... An array, in contrast, may be accessed at any point: ... PICK and ROLL are therefore array operations, not stack ... can be factored from GET-ORDER SET-ORDER: ...
    (comp.lang.forth)
  • Re: I dont understand the definition of DOES>
    ... on stack on code entry). ... You can use DOES to make ARRAY and then use ARRAY ... CREATE CELLS ALLOT ... First you'd make the code that will execute on the child word. ...
    (comp.lang.forth)