Dc: how do I put (and delete) the top stack number?

In dc, how do I pop and drop a number from the top of the stack? A stack with three elements ( 1 2 3 ) should become a stack with two elements ( 2 3 ). I am currently dragging a number onto another stack (Sz), but that seems pretty lame.

+4
source share
2 answers

There are many ways to remove the top of the stack, but they have side effects. Removing an item without side effects requires that you not include side effects.

To remove the top of the stack without a side effect, make sure the top number is a number, and then run d!=z If there was [5] on the stack, this does the following

  • Start with the item to delete. Stack: [5]
  • Duplicate the top of the stack. Stack: [5,5]
  • Press top 2 and check if they are equal: 5 != 5 Stack: []
  • If the test passes (which it cannot), run z Stack: []

To make sure the top of the stack is a number, I use z , which will calculate the length of the string or the number of digits in the number and push it back. There are other options, such as X. Everything that makes a number out of anything will work so that it is compatible with! =.

Thus, the complete answer for inserting copies in all situations:

 Zd!=r 

I usually put this in the D register (for Drop):

 [Zd!=r]sD 

and then I can run

 lDx 
+5
source

[... the answer is deleted, another answer is better ...]

+4
source

Source: https://habr.com/ru/post/1487574/


All Articles