Latest update May 8, 2015.

To prevent spambots from collecting mail addresses, all mail addresses on this page have the "@" replaced by "#".

Assignment 4 - A Calculator with a GUI

In this assignment you will create a calculator with a GUI. It must have the following features:

In addition, the following is required:

Also give some thought to how to handle situations with erroneous inputs, for instance if two operators are input right after each other. (No advanced error handling is required, but the behaviour of your program should not be erratic.)

The semantics for expressions should be the usual "left-to-right" semantics used by calculators, where an operator that is input takes the currently input expression as left-hand side argument, and the next input value as the right-hand side argument. Thus, the input 3*2+55/0.2+4 should be interpreted as (((3*2)+55)/0.2)+4. Note that this is different from the interpretation that results if the usual precedence rules for arithmetic operators are applied.

Hint: It might save you some work to re-use the expression trees from Lab 2.

The focus of the assignment is really on how to structure a reactive program, using the features of a language like F#, rather than creating a beautiful GUI. A reactive program reacts to things happening in its environment, like some data becoming available or some user input taking place. A GUI is an example of a reactive program, but by no means the only one: for instance, many embedded systems are reactive.

In F#, reactive programs are usually based on events and event handlers. To make your task of writing the reactive part of the program easier we have provided you with a code template for a fairly generic event handling loop, which interprets event occurrences as they appear and provides the proper handling. You should actually not have to write a lot of code on your own to obtain a solution.

Unfortunately there are some issues with the builtin IEvent<'a> data type in F# that in some situations can give rise to so-called memory leaks. More recent versions of F# and Visual Studio can instead use the Fsharpx library. The easiest way of installing the Fsharpx core is to run the command 'Install-package Fsharpx.Core' in the NuGet package manager console. In Visual Studio this can be found under Tools->NuGet Package Manager->Packet Manager Console. Alternatively you can download the source from and build it, if you use a platform where the NuGet packet manager is not supported.

A code template for the event handling, which is based on the Fsharpx library, is found here. You can either add the .zip archive to "\My Documents\Visual Studio Version\Templates\ProjectTemplates\" (F# doesn't have a language specific folder, so just place it in this directory), or create a new project and add the .fs files. Note: if you create a new project you need to install Fsharpx.Core (as described above) and add a reference to WinForms (Project -> Add Reference -> System.Windows.Forms).

For those of you that have an older version of F# or Visual Studio, we have made an alternative code template that is based on IEvent<'a>. It is found here. You will probably have to use this template if you develop your solution on the lab computers at MDH. (Our apologies for this inconvenience.)

You are free to skip the code templates if you wish, and develop your own solution. However, then be prepared to explain in some more detail how your solution works. We will demand that your solution is "declarative style", without excessive use of imperative features.

Björn Lisper