In many programming languages there are functions that remove an element from a set while giving back that element. E.g. I'd like to have a function FetchFromStack
that does the following:
stack = {one, two, three};
element = FetchFromStack[stack]
stack
one
{two, three}
Of course I can implement it like:
If[stack==={}, element=False, element = stack[[1]]]
If[Length[stack]>1, stack = stack[[2;;]], stack={} ]
but this seems much too hacky of a solution to me. Is there a proper routine in Mathematica that does this? If not, what would be the best way to implement it?
stack == {}
? $\endgroup$fetchFromStack
should returnFalse
. $\endgroup$