Re: [stack] A question on joy syntax.

Taoufik Dachraoui <[email protected]>
Newsgroups gmane.comp.lang.concatenative
Message-ID <[email protected]>
looking at the defintion of factorial:

in Joy:
fact == [0 =] [pop 1] [dup 1 - fact *] ifte

in v:
[fact
     [dup 0 =]
         [pop 1]
         [dup 1 - fact *]
     ifte].

In the second definition we explicitly manipulate the stack (dup) so  
that the remaining code finds the stack in the expected state. In  
this case dup is certainly faster than saving and restoring the  
stack, but in other situations this may not be true.

Taoufik


On Mar 3, 2007, at 11:59 AM, Taoufik Dachraoui wrote:

> I tried to define ifte as it is implemented in joy1 and I found this:
>
> [T] [A] [B]
> ifte == [[[stack] dip] dip
> [dip swap] dip] dip #save stack and run T
> choice [unstack] dip i; # restore stack and choose
> between A and B
>
> The stack is saved before the execution of [T] and restored before the
> execution of [A] or [B]. The test in this case is non destructive;
> the stack
> is unchanged before choosing between A and B.
>
> The way you implmented ifte is as follows:
>
> [T] [A] [B]
> ifte == [[i] dip] dip #run T
> choice i; # choose between A and B
>
> Obviously your implementation is faster, but the test in this case
> is destructive; the stack can be modified greatly depending on T.
>
> Is it more difficult or easier to reason with destructive tests?
>
> Taoufik
>
> On Mar 1, 2007, at 9:17 PM, Rahul wrote:
>
> > I have been looking through the joy papers, and have
> > this confusion:
> >
> > The help on ifte says:
> >
> > ifte [B] [T] [F] -> ...
> > Executes B. If that yields true, then executes T
> > else executes F.
> >
> > Now the help on = says:
> > = X Y -> B
> > Either both X and Y are numeric or both are strings
> > or symbols. Tests whether X equal to Y. Also supports
> > float.
> >
> > I assumed that '=' will consume two arguments off the stack,
> > and leave the true or false on top.
> >
> > the joy interp seems to support this.
> >
> > 1 2 = .
> > false
> > .
> >
> > Now, if I use the same inside an ifte
> > 1 5 [1 =] [dup *] [dup +] ifte .
> > 10
> >
> > The doubt I have is this:
> > as soon as [1 =] is executed, I would expect '5' off the stack,
> > so the [dup +] should have actually found '1' on the stack and given
> > me 2.
> >
> > I checked this too: which seems to do fine.
> > 5 [true] [dup *] [dup +] ifte .
> > 25
> >
> > Is there a reason for this? Is for some reason the stack
> > invariant when executing ifte condition?
> > The below seems to verify it.
> >
> > 5 6 [pop 1 =] [dup *] [dup +] ifte .
> > 12
> > .
> > 5
> >
> > But I cant find any info on this.
> > The other quoted programs does not seem to share the invariant stack
> > behavior:
> > ================================
> > i [P] -> ...
> > Executes P. So, [P] i == P.
> >
> > 1 2 [1 +] i .
> > 3
> > .
> > 1
> >
> > Could some one please explain why this is so? or guide me to the
> > docs that explains it?
> >
> > rahul.
> >
> >
> >
>
> [Non-text portions of this message have been removed]
>
>
> 



[Non-text portions of this message have been removed]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.