[stack] Re: A question on joy syntax.
"Rahul" <[email protected]>
| Newsgroups | gmane.comp.lang.concatenative |
|---|---|
| Message-ID | <[email protected]> |
Taoufik Dachraoui <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. The speed is probably not important in this case. I had not expected a change in behavior for a quote when it is inside an 'ifte' and when it is evaluted outside by an 'i'. This behavior gives the last param to 'ifte' a kind of special status (The stack and unstack are quite meta compared to the rest of the words). > Is it more difficult or easier to reason with destructive tests? You are right here. My contention was that it did not seem to fit nicely with the rest of joy. Looking at it again, I like the way the Cat language has taken. It expects a boolean rather than a quote at the top of the stack, The result is not different from what we get in joy, though it avoids the meta operations. [snip..] > > The help on ifte says: > > > > ifte [B] [T] [F] -> ... > > Executes B. If that yields true, then executes T > > else executes F. [snip..] > > I assumed that '=' will consume two arguments off the stack, > > and leave the true or false on top. [snip..] > > 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.