Exploring Uiua

- uiua

I recently got quite interested in a relatively new programming language called Uiua (pronounced “wee-wuh”), which its creator Kai Schmidt describes as follows:

Uiua is a general purpose, stack-based, array-oriented programming language with a focus on simplicity, beauty, and tacit code.

I’ve long been fascinated by array languages (like APL, J, or BQN), as well as stack languages like FORTH and Factor. Combining these two paradigms leads to very interesting and — in my opinion at least — beautiful code, which I find much more readable than that of comparable languages.


Let’s look at a few examples of Uiua code.

A sum function

∑ ← /+

On the first line, we define a simple sum function and bind () it to the glyph (Uiua identifiers can either be strings of characters or a single Unicode glyph). It uses the reduce modifier (/) with the add function (+). On the second line, we use range () to create an array of all the natural numbers from 0 to 10, which we then sum. So while Uiua code is parsed from left to right, it executes from right to left. That means the execution goes like this:

  1. 11 is put on the stack.
  2. pops the 11 off the stack and pushes the array [0 1 2 3 4 5 6 7 8 9 10] onto it.
  3. The function pops the array off and replaces it with the result of the summation, 55.

In a standalone Uiua program, the remaining stack values are automatically printed, so we’re done.


We can use very similar code to define a simple factorial function:

Fac ← /×+1⇡

We create a range () up to the stack’s top value, then add 1 to every element. Afterwards we use the reduce modifier (/) again, though this time with multiply (×) as the argument. Let’s run through it step by step again:

  1. The line Fac5 puts 5 on the stack, then calls Fac.
  2. The 5 gets popped off the stack and replaced by the array [0 1 2 3 4].
  3. The +1 replaces this with the array [1 2 3 4 5].
  4. multiplies all the numbers and puts 120 on the stack.

Arithmetic mean

Now, a slightly more interesting example, a function for calculating the arithmetic mean:

Mean ← ÷⊃⧻∑

⍤∶≍, NaN Mean[]
⍤∶≍, 3 Mean⇡7

The most interesting function here is fork (), which calls two functions on the same value. In our case those two functions are the previously defined summation function (), as well as length (). So ⊃⧻∑ will pop an array off the stack and push the sum of its elements and the length of the array onto the stack. It will then divide (÷) the former by the latter, to get the mean.

What’s interesting here is the code between the --- at the end. These are tests, which will be executed when running a .ua file with the uiua test or uiua watch commands.

Difference of squares

For the next example, here’s a Uiua solution to Exercism’s Difference of Squares exercise:

Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.

The code reads almost exactly like the problem definition:

SumOfSquares ← ∑ⁿ2
SquareOfSum ← ⁿ2∑
DiffOfSquares ← -⊃SumOfSquares SquareOfSum ⇡

⍤∶≍, 2640 DiffOfSquares11

The only new function here is power (), which is quite self-explanatory. Another way to square a number would be the combination of duplicate (.) and × (multiply), e.g. ×.5.

Project Euler problem 1

For something a bit more interesting, here’s a solution to the first problem of Project Euler:

Find the sum of all the multiples of 3 or 5 below 1000.


There’s quite a bit going on here, so let’s look at it in more detail: first we build a range of the natural numbers below 1000 (). We then put 3 on the stack, and use over (,) to push another copy of the array, before pushing the value 5 onto the stack, which now looks like this:

[0, 1, 2 .... 999]
[0, 1, 2 .... 999]

After this we use the both modifier () to call the modulus function () on the two sets of values. We then combine the two resulting arrays with minimum (), resulting in a single array where all elements that divide by either 3 or 5 are set to 0. We turn this into a boolean array (where 1 is true and 0 is false) by comparing to 0 (=0). This array is passed to where () to get the elements themselves, which we then sum with (which is my custom function for /+).

Beyond simple coding problems

If you’re now thinking of Uiua as a fancy calculator, I wouldn’t blame you, as I haven’t mastered the language enough yet to come up with more interesting examples by myself. But its author definitely intended to make it useful for all kinds of programs, so the language also includes functions for accessing the filesystem, the process environment, TCP sockets and much more (see the full function list). He even wrote a simple HTTP server in Uiua, which is good enough to host the language’s web site.


Uiua resources