Caterpillar

Daily Thought - 2024-05-04

< back to list

Yesterday, I said that I only want to provide postfix operator in Caterpillar. I'd like to explain why I like them, starting with simple math operations. If we want to add two numbers, we'd typically write this with an infix operator: 1 + 2. In postfix, this would be 1 2 +.

This might look unfamiliar, but it's extremely simple to work with: Just start at the left. There's one value (1), then there's another (2), and then we add those (+). If we want to multiply the result by 3, we can write this as 1 2 + 3 * (infix: (1 + 2) * 3). Multiplying first is easy too: 2 3 * 1 + (infix: 1 + 2 * 3).

This exposes a neat thing about postfix operators: You never need parentheses, nor is there any operator precedence. It always goes left to right. 1 and 2 added is 3. 3 and 3 multiplied is 9. Or in the second example, 2 and 3 multiplied is 6. 6 and 1 added is 7.

<< previous thoughtnext thought >>