Esolang highlight: ><>

Conor O'Brien
Programming Puzzles & Code Golf
6 min readOct 16, 2016

In the realm of esolangs made before this decade, ><> is one of my favorites. It’s two-dimensional, meaning that the code layout resembles a field. You can try it out online at https://fishlanguage.com/playground, or download an interpreter from here. (I suggest the one written in python.) If you want to use the python interpreter’s behaviour, try this Try it online! link.

Citation: a lot of the terminology and reference contained here is from the esolangs article.

Code layout and execution

><> operates on a grid, or codebox. On this grid, there are characters; each character performs an operation when the instruction pointer (abbreviated IP for convenience) reads them. The IP starts at the top-left of the code, and starts out reading characters from left to right. The direction can be changed by one of these characters:

> < ^ v

These characters change the direction to move right, left, up, and down, respectively. Now, the pointer continues to move and read characters until it encounters a “;” character. When it does, the program terminates. Now, consider the following program:

v ; <>   ^

The pointer starts at the first “v”, moves downwards to the “>”, rightwards to the “^”, upwards to the “<”, and leftwards to the “;”, finally terminating. You can see this in action in the following GIF:

Recorded from https://fishlanguage.com/playground

The red square represents the pointer and which instruction it’s reading. This is the essence of ><> — constructing code that utilizes the code field effectively.

Important note: when the pointer hits a “wall” of the code box, it wraps around to the other side.

In addition to the four cardinal pointers, there are five mirrors:

| _ # \ /

Mirrors can be described as “reflective”; as opposed to pointers, they do not have a constant behaviour when encountered by the IP. In order to describe them succinctly, I’m going to redefine some things.

Remember when I said the IP started at the top-left of the code? Let’s say that it starts out at (0, 0). The point to its right would be (1, 0), etc. It’s an (x, y) coordinate, where x represents the character and y the line. The pointer moves (dx, dy) units per step. Call this the direction vector (DV for short). (A step would be each time the pointer moves, as seen the GIF above.) That is to say, it moves dx units along the x-axis and dy units along the y-axis.

Okay, so having done that, we can express each of the mirrors’ effects with a simple translation:

Mirror | Effect on DV
| | (dx, dy) => (-dx, dy)
_ | (dx, dy) => ( dx, -dy)
# | (dx, dy) => (-dx, -dy)
/ | (dx, dy) => (-dy, -dx)
\ | (dx, dy) => ( dy, dx)

Finally, I will discuss a rather important command, the “!”. This skips the over next instruction. So, this program actually never terminates, since the “;” is skipped over:

v  <
>!;^

You think you have it down? Here is a program containing all of the mirrors and pointers, along with code box wrapping and “!”s:

v /    v
! > v!
|
\ |/!
^ <
_
;# !<
!

Here’s a GIF to help you:

It’s pretty complex. Also pretty.

One command I did not mention is the “x” command. It simply goes in a random cardinal direction.

That’s pretty cool, but can I actually program in this?

Yes! So far I’ve only told you about some control flow commands. There are, in fact, many other commands. ><>’s data layout is a stack of numbers, initially empty. You can push a number from 0 to 15 to this stack using the following commands (respectively):

0 1 2 3 4 5 6 7 8 9 a b c d e f

You can also perform common operations (b is the top of the stack, a is the one below b; both are popped from the stack after the command is read):

operator | effect
+ | a + b
- | a - b
* | a * b
, | a / b
% | a % b or a mod b
= | 1, if a == b; 0 otherwise
( | 1, if a < b; 0 otherwise
) | 1, if a > b; 0 otherwise

With the introduction of numbers, we can talk about two new control-flow commands: “.” and “?”.

One of the single-most important commands is the “?” command. It pops a value a off the stack; unless a = 0, the next instruction is executed. Otherwise, it’s skipped. This is how you will emulate if statements and loops.

As for “.”, it pops y, x off the stack and moves to (x, y) in the codebox.

In addition to the aforementioned operators, there are operators that are used to manipulate the stack itself. I’ll quote the esolangs article mentioned earlier for these (this is formatted differently, however.):

 :    | Duplicate the top value on the stack.
~ | Remove the top value from the stack.
$ | Swap the top two values on the stack
@ | Swap the top three values on the stack, shifting them
rightwards (e.g. if your stack is 1,2,3,4, calling @
results in 1,4,2,3)
} { | Shift the entire stack to the right and left, respectively.
(e.g. when having 1,2,3,4, calling } results in 4,1,2,3
while { results in 2,3,4,1)
r | Reverse the stack.
l | Push the length of the stack onto the stack.
[ | Pop x off the stack and create a new stack, moving x values from the old stack onto the new one. See Stacks.
] | Remove the current stack, moving its values to the top of
the underlying stack.

As for input and output, you can use “i” to read a byte of input, “o” to output a character byte, and “n” to output a number.

While ><> has no concept of variables, it does have a register. This holds a single value. You can write to the register using the “&” command; using the “&” command after a write will read the register. Then, you can write to the register again. When interacting with layers of stacks, each stack starts out with an empty register.

Another cool feature is that ><> has reflection — it can read and write to its own source code. To get a character at (x, y), the code is this:

x y g

Similarly, to write a character c to position (x, y), use this:

c x y p

One final note: you can actually use strings in ><>. These are demarcated by these two marks:

" '

Specifically, those characters enable “string mode”, in which the character code of each character that the IP reads is pushed to the stack; this process continues until the IP reads a matching quote mark.

Now what?

I’m going to show you some example programs that I wrote. These complete conventional programming tasks. I won’t be using GIFs for these, but you can always test them out at https://fishlanguage.com/playground. Also, these solutions are not golfed/minimized, but are intended to be readable. I encourage you, the reader, to work through each code snippet and figure out how it works.

Hello, World!

"Hello, World!"r v o <
> l?^ ;

Random number between 1 and 4

12. >n;
!
1x3^
4
> ^
2

Factorial

:2(?v 1$ >:1=?!v~n;
\1n; :
^-1r*r<

(Assumes the input value is on the stack. Input using the “initial stack” online or with the -v flag in the python interpreter.)

Extendable Chat Interpreter

That’s right. I spent way too long making this, but I did. Not only does this thing parse commands, but it also extendable, meaning that it’s really easy to add new commands. Without further ado… here it is!

That’s a nice way to end a blog post. Have fun, and don’t lose your mind.

--

--