Calculating

Calculating

I’ve been trying to solidify a concept for a RPN-style calculator in my head for a while now, I figured it might be good to try and actually write down some example uses to cement it.

The main feature I wanted to add was some FP-esque list operations (ie. map, fold) that would operate on your existing stack; just because I have in the past thought “wow it’d be nice if I didn’t need to mash the ‘+’ key to add all these numbers”.

Basics

It’d be the same as any RPN calculator for the basics.

1           ; stack = 1
2 3 +       ; stack = 1, 5
+           ; stack = 6
10 -        ; stack = -4

Variables

It’d probably be nice to have variables too? I’m not sure about how I’d do the syntax here.

1 2 3        ; stack = 1, 2, 3  |
=> a         ; stack = 1, 2     | a = 3
a            ; stack = 1, 2, 3  | a = 3

List operations

Again, the syntax is totally up in the air.

For this example I’ve arbitrarily picked := for map, << for foldr and >> for foldl.

1 2 3        ; stack = 1, 2, 3
:= (1 +)     ; stack = 2, 3, 4
<< *         ; stack = (4 * 3 * 2) = 24
1 + 5 2      ; stack = 25, 5, 2
>> /         ; stack = (25 / 5 / 2) = 2.5

Does this already exist?

Probably – if not the answer would be ‘just use language x’s REPL’ – but it seems like it could be a fun weekend-y project.

I’d kind-of like to learn some Lisp or Scheme anyway and they seem like good languages to do this sort of thing in?

Alternatively dc seems like it might already be able to do this – or at least a reasonable place to fork from.