lunedì 18 ottobre 2010

RPN calculator (without custom stack)

I finally managed to write an RPN Calculator without a stack, except the call stack.

I build two diffirent solution:

  • v0.1 That use two different data type (Computation and Expression) to parse recursively the expression's tokens. This is a bit more convoluted than v0.2 but could probably be simplified with a Writer.
  • v0.2 That use foldr and Computation and hack function closures (from an idea suggested by Pieter Wuille).
    Actually, this is a bit like a trickery, since it use a kind of stack of function embedded in closures instead of a stack of value.
    BTW it's funny enought to be proud about it. :-)

I'm also thinking about a v0.3 with a different approact, but for now I've played enough.

Of course, using a custom stack (like a list), would reduce the code to 10 to 15 lines.
But it would not be such a funny hack!

Nessun commento:

Posta un commento