Factoid:
In Brain-Flak a one is shorter than zero and multiplying by seven takes more characters than multiplying by eight.
Since Brain-Flak programs must have an even number of characters I have added some programs with extra spaces for the odd length snippets. These are not well golfed intentionally but rather put there in an attempt to give you a little more for your kind upvotes.
###Length 2 snippet
<>
It switches to the offstack. As a single program this will always output nothing regardless of the inputs.
###Length 3 snippet
{ }
This is the {}
nilad it pops top item on the stack and returns its value. As a full program it just removes the last item input.
###Length 4 snippet
(())
This program pushes one to the top of the stack. This is the shortest way to express 1 in Brain-Flak.
###Length 5 snippet
([ ])
This program pushes the stack height to the top of the stack. At the start of a program it acts like argc
.
###Length 6 snippet
({}{})
This program adds two numbers.
It pops the top two elements and pushes the sum.
###Length 7 snippet
This one requires a -d
flag to run so +3 bytes
(This one also doesn't work on try it online)
@ij
This is the injection flag! (With a space after it because it needs to be length 7)
This halts the program takes a Brain-Flak program from STDIN and runs it as part of the code. Its my favorite flag and (arguably) Brain-Flak's shortest self interpreter. @ij
flags can be nested
###Length 8 snippet
({}<>)<>
This snippet moves the top of the current stack to the other stack. It is a useful component of many programs. It starts by opening a push with (
pops the first value with {}
, moves to the other stack using <>
and puts the popped value down using )
. When that is done it moves back to the original stack with a <>
.
###Length 9 snippet
({}[()])
(One space after the program makes it 9 characters)
This snippet decrements the top of the stack by one. It works by popping the top of the stack with {}
and adding it to the negative of ()
(-1) and then pushing the result back on the stack.
###Length 10 snippet
{({}<>)<>}
This snippet will move values from one stack to another until it encounters a zero. In many programs where it is known that the stack does not contain a zero this is used as a cheap stack reverse.
###Length 12 snippet
({}<({}())>)
We've finally gotten long enough snippets that we can show off the usefulness of the <...>
monad. This program increments the number underneath the top of the stack, while leaving the TOS intact. This approach can be extended. For example, this will increment the number second from the top of the stack:
({}<({}<({}())>)>)
In general, you can do:
'({}<' * n + <code> + '>)' * n
to run <code>
on the number that's n from the top without affecting the rest of the stack.