Stack-based languages don't necessarily fit in the pseudo-code model that constitutes our definition of truthy and falsy:
if (x) {
print "x is truthy";
}
else {
print "x is falsy";
}
The closest equivalent to x in a stack-based language would be the top of the stack (assuming this is what the conditional inspects), but in a sense, it really inspects the entire stack (although it does ignore everything but the top).
Languages like Brain-Flak don't make matters easier; the closest thing Brain-Flak has to a conditional is the {...}
loop which executes ...
while the top of the stack is truthy and halts when it's falsy.
It's rather easy to emulate a conditional (sans the else
part) using while loops. Since a non-zero integer on top of the stack permits us to enter the loop while 0 on top of the stack prevents it, I think it's rather clear that non-zero integers should be treated as truthy and 0 as falsy. (If you disagree, please say so.) So far, so good.
Now, at the end of the program, Brain-Flak prints the entire stack (and this is the only way of output), so it isn't really possible to print a single integer, but only a stack of height 1. Does that mean that the entire output should be treated as a stack and its truthiness should depend only on the top of the stack?
This has come up twice recently:
In this answer, I'm treating an empty stack as falsy. Since Brain-Flak's stacks have an infinity of zeroes at the bottom,
{...}
would not enter the loop on an empty stack.I did this without thinking twice about it, probably because treating empty output as falsy is rather common in other languages.
I got a golfing suggestion on this answer that proposed printing
1 0
as truthy output. The argument is that1 0
is truthy since{...}
sees only the top of the stack.While – now that I've thought about it – it seems to be quite similar to treating an empty stack as falsy, it seems weird to output two values and disregard all but the first one.
One the other hand, Brain-Flak's stacks could be considered array-like, and each programming language has its own ways of determining an array's truthiness, e.g., an array is truthy in Python iff it is non-empty, while an array in truthy in Octave iff it is non-empty and doesn't contain a 0.
Thoughts?
3\n
doesn't represent the integer3
, but the stack/array[3]
. \$\endgroup\$