1. Description

One of the earliest Programming Praxis challenges is called RPN Calculator . Our task is to write a RPN module capable of evaluating simple RPN expressions and return the right result . The exact requirment:

Implement an RPN calculator that takes an expression like 19 2.14 + 4.5 2 4.3 / - * which is usually expressed as (19 + 2.14) * (4.5 - 2 / 4.3) and responds with 85.2974. The program should read expressions from standard input and print the top of the stack to standard output when a newline is encountered. The program should retain the state of the operand stack between expressions.

Programming Praxis

The natural algorithm to resolve this exercise is stack-based :

The first step is to tokenize the expression .If the expression is “19 2.14 + 4.5 2 4.3 / – *” after the tokenization the resulting structure will be an array of operands and operators, for example an array of Strings {“19”, “2.14”, “+”, “4.5”, “2”, “4.3”, “/”, “-“, “*”} .
At the second step we iterate over the array of tokens .If the token:

  • Is an operand (number, variable) – we push in onto the stack .
  • Is an operator (we apriopri know the operator takes N arguments), we pop N values from the stack, we evaluate the operator with those values as input parameters, we push the result back onto the stack .
If the RPN expression is valid, after we apply the previous steps, the stack would contain only one value, which is the result of the calculation .

2. Implementation

I will implement the solutions for this challenge using three different languages: Scala, python and Java .

2.1 Scala Solution

And if we compile and run the above code: Continue reading

After a failed attempt to grasp Haskell, but somewhat seduced by the concise and elegant ways of Functional Programming, I’ve started to look for alternatives . Knowing Java, and having an everyday interaction with the JVM the obvious choices were Clojure or Scala . I never had the chance to try Clojure, probably because right now I enjoy Scala so much (the butterflies…) . Of course I cannot say that Scala scales, or if Scala is the next Java, or Scala is the answer, but from what I’ve experienced so far, things look good and I am optimistic that some day Scala will receive the attention it deserves .

There are a lot good books on Scala, but probably the most popular is “Programming in Scala, A comprehensive step-by-step guide” by Martin Odersky (Scala’s creator), Lex Spoon, and Bill Venners .

In a chapter there, the authors give us an insight on how to create Functional Objects -> objects that are immutable . The materialization of the concept is a Rational class, that encapsulates the idea of rational numbers . It allows us to be add, subtract, multiply, compare etc. rational numbers .  At some point in the example the fraction is reduced to the lowest terms.

Reducing the fraction to its lowest terms  (also known as normalization) is probably one of the simplest programming exercises we all start with . The actual problem is to determine the greatest common divisor of the fraction’s denominator and numerator, and divide those numbers by it . Not long ago I’ve written a solution for this exercise in C, but we all know C is far from being an elegant peace of technology.

Inspired (a lot!) by the example in the book, I’ve come with this solution:

Fraction.scala

MainObject.scala

To compile and run the above we can use scalac (the equivalent of javac) as: Continue reading