From UBC Wiki


Authors: Lydia Li, Emrys Yang

What is the problem?

We will attempt to generate a simple non-graphing calculator that can do basic operations (Addition, Subtraction, Multiplication, and Division) using Haskell.

What is the something extra?

- We will attempt to see if we can use more complex operations other than addition, subtraction, multiplication, and division in the calculator. Possible operations are listed below:

  • Module.
     Example: we can do calculations like  20 mod 3 = 2  and  100 mod 30 + 3 = 13 ...
  • Percentage.
     Example: we can do calculations like  80 % = 0.8  and  1000 % = 10.0 ...
  • Exponentiation / Power.
     Example: we can do calculations like  4 ^ 2 = 16  and  3 ^ 3 + 3 = 30 ...
  • Square Root.
     Example: we can do calculations like  9 - sqrt(9) = 6  or  9 - 2√(9) = 6 ...   
  • Logarithms.
     Example: we can do calculations like  (log2(4) + 10 + 12) = 24 ...
  • Dot (decimal number).
     Example: we can do calculations like  (3.125 + 10.34 + 12.875) = 26.34 ...

- We will attempt to see if we can create a Haskell GUI (Graphical user interface) for our calculator. The appearance of the GUI will be similar to the simple calculator on our phones:

A sample calculator GUI

What did we learn from doing this?

In this project, we found that using Haskell for a simple calculator with GUI is feasible but have some inconvenient parts.The logic of our calculator is very straightforward. It is similar to what we did in out first Prolog project, the Math 24 solver.

On the one side, it makes the expression easier and faster, and more particularly in the following aspects:

  • Haskell only uses functions as its units to construct a complete computation process. It builds a recursive state which keeps repeating the previous step, and base on this state, generates possible variables and results of next step, and then repeat the previous step. In our calculator, once we define one operator and how it displays on the interface, all other operators will follow the same process and format. This saves us a lot of time and
  • Although the implementation of the modules that GUI needs is not easy to start, the whole process of creating a GUI application in Haskell does not require the users to have strong knowledge of Haskell except a very basic understanding of how to work with the IO monad which we already learned in class.

On the other hand, Haskell is not convenient enough in its formatting and GUI building:

  • Haskell is very "sensitive". Any change in indentation will have a significant impact on our results. For instance, anything that writes in the main function needs indentation, or it will be regarded as "things that are not included in the main function" and gives an error message. Besides, in Haskell, expressions that are "paralleled" with each other (execute at the same time) must be aligned. If not, the result may have huge differences.
  • The start of the GUI implementation is quite difficult: most packages that we downloaded online are incomplete and not user-friendly. To build a graphical user interface application in Haskell using bindings to GTK+, we need to try a large amount of different packages (like the GTK3 and GTK-graph we tried before) and test over and over again to make sure the package is usable and can display a complete graphical user interface. Because of this, the start of building a GUI takes us lots of time.

This is the final version of our calculator's GUI (except basic operations, we also add Dot, Module, Percentage, and Exponentiation to the calculator):

Calculator by Emrys and Lydia

Code Submission

Link to our code: Calculator by Emrys and Lydia

Package Source

Link to GTK3: The gtk3 package

LInk to source: Compilation error #stdin compilation error #stdout