10
\$\begingroup\$

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 that 1 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?

\$\endgroup\$
4
  • \$\begingroup\$ What do you mean by "so it isn't really possible to print a single integer, but only a stack of height 1"? \$\endgroup\$
    – Wheat Wizard Mod
    Commented Oct 6, 2016 at 2:35
  • 2
    \$\begingroup\$ In a sense, the output 3\n doesn't represent the integer 3, but the stack/array [3]. \$\endgroup\$
    – Dennis
    Commented Oct 6, 2016 at 2:40
  • 5
    \$\begingroup\$ I'm not gonna post this as a full answer unless I can come up with some good reasoning/arguments, but my gut says that the first example (empty stack) should be fine since many languages use the empty string as falsy. But I don't really think printing multiple integers should be truthy or falsy in brain-flak specifically. Mostly because most challenges say "Print a truthy/falsy value". Printing an array makes sense in something like octave, because it's still a single truthy/falsy unit, which is not so in bflak. \$\endgroup\$
    – DJMcMayhem
    Commented Oct 6, 2016 at 13:49
  • 1
    \$\begingroup\$ My two cents: challenges should avoid requesting truthy or falsy values in favor of any two distinct outputs. \$\endgroup\$
    – feersum
    Commented Oct 13, 2016 at 7:04

1 Answer 1

6
\$\begingroup\$

We should only consider the top of the stack

If we were to implement the pseudo code in Brain-Flak it would look like:

(({})){(<{}
 ({}<
  ((((((()()()()())){}{}){}){}){}())((((((()()()){}){}()){}){}){})((((((()()()){}()){}){}()){}){})(((((((()()()){}){}())){}{})){}{})((((((()()()){}()){}){}){}()){})((((((()()()){}()){}){}()){}){})((((()()()()){}){}){})((((((()()()){}()){}){}){}()){}())((((()()()){}()){}){({}[()])}{})((((()()()()){}){}){})((((((()()()()())){}{}){}){}){})
 >)
>)}{}
({}<(())>){((<{}{}>))}{}
{(<{}
 ((((((()()()()())){}{}){}){}){}())((((((()()()){}()){}){}){}()){}())(((((((()()())){}{})){}{}){}){})((((((()()()){}){}){}){}){}())((((((()()()()){}){}())){}{}){})((((()()()()){}){}){})((((((()()()){}()){}){}){}()){}())((((()()()){}()){}){({}[()])}{})((((()()()()){}){}){})((((((()()()()())){}{}){}){}){})
>)}{}

Try it Online

While it may not look like the pseudo code provided, this is a standard if/else idiom in Brain-Flak. The community has agreed on this pseudo code so we should follow it.

This would mean that truthiness depends on only the top value of the active stack at the end of the program.

As for other stack based languages I think that we should attempt to emulate the pseudo code as closely as possible.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .