How to put the Nth item on the stack [Befunge-93]

If you have a befunge program 321&

, how would you access the first item (3) without throwing away the two second items?

The instruction \

allows you to toggle the first two elements, but that doesn't bring me closer to the last ...

The current method I am using is to use the command p

to write the entire stack into program memory to move to the last item. For example,

32110p20p.20g10g@

      

However, I feel like this is not as elegant as it could be ... Is there no technique to push the first element onto the stack as N, pop the Nth element off the stack and push it at the top?

(No, this is a perfectly acceptable answer)

+3


source to share


1 answer


Not really. Your code can be shortened to

32110p\.10g@

      

But if you want a more general result, something like the following might work. Below I use Befunge as it was meant to be used (at least in my opinion): as a functional programming language, with each function getting its own set of rows and columns. Pointers are created using directivity and are stored 1

and 0

identify where the function was called. One thing I would like to point out is that the stack is not meant to be stored in almost any language. Just write the stack to storage. Note that 987

the stop overflows with a length of 10.



v           >>>>>>>>>>>12p:10p11pv
            1    0   v<<<<<<<<<<<<<<<<<<
                     v   >210gp10g1-10p^
                     >10g|
                         >14p010pv
                     v<<<<<<<<<<<<<<<<<<<<<<<<
                     v       >210g1+g10g1+10p^
                     >10g11g-|
               v       g21g41<
             v _ v
>98765432102^>.  2^>.@

      

The above code writes up to and pops the nth element on the stack into "memory", writes the nth element elsewhere, reads "memory", and then pops the nth element onto the stack. The function is called twice on the bottom line of this program.

+2


source







All Articles