Excel as a Programming Language

Spreadsheets were a formative element of my introduction to programming. I learned them while learning programming and ideas from each influenced my understanding of the other. Generally, since I programmed a lot more, I tended to think in how a standard structural/imperative language would do things, and my spreadsheets started to have more and more complex macros.

Looking back though, what is interesting is that most spreadsheets are a pure functional system. I would say that Excel is the most deployed functionally programmed system. The biggest impediment of an Excel pro to learning conventional programming might be understanding the stateful, imperative, mutating model in the most common application programming languages and frameworks.

For example, if Excel were a programming language, you might say something like

A1 = 3.0
B1 = A1 * 2.0
A1 = 4.0
print(B1)

And you would naturally expect this to print 8.0.

The way to write it is more like this (in Swift)

var A1 = { 3.0 }
var B1 = { A1() * 2.0 }
A1 = { 4.0 }
print(B1())

This prints 8.0 as expected.

In other words, all cells are functions, not expressions or constants. Excel just simplifies your code by wrapping them for you.

Another way would be to use reactive programming, like Combine

let A1 = CurrentValueSubject<Double, Never>(3.0)
let B1 = A1.map { $0 * 2 }
A1.send(4.0)
B1.sink { print ($0) }

Simple, right? It is more in the spirit of what is happening in Excel and lets us use constants instead of variables.

It’s no wonder Excel is popular.