Haskell in web browser/Calc example
Conclusion: all together
In conclusion, let's finally develop a program that does something useful. As another "classical" example, this will be a RPN calculator.
The main Widget,
mainW consists of the following pieces:
- Two Message Boxes. One (
calcmb) will be used to transmit keystrokes and mouse clicks received from the browser's input facilities. Another (
dispmb) will be used to update the calculator's display element.
- A <DIV> element with visible border: it encloses all other elements of the calculator. It is also responsible for receiving keyboard input.
The following elements are nested within the above mentioned <DIV>:
- A <DIV> element that contains the calculator's display.
- 16 buttons for mouse input. Note the use of
map. All buttons use the same Message Box
calcmb, so it is passed to each button's creation function. Each button uses its own character taken from the
btxtlist: button creation function is mapped over this list. Finally, the resulting list is folded with
+++to sequence all buttons properly.
- Activator which implements the internal (stateful) logic of the calculator. It is based on
passMapStateAwhich receives messages from its input Message Box, maintains internal state transitions based on what is received, and maps internal state to messages sent to the output Message Box. That's how the two parts (user input and result display) are wired together.
- Activator based on
evtBCastAto catch keystrokes and route them to the same Message Box where mouse clicks go.
- Activator based on
tabIndexA. Although DOM does not define the tabIndex property for most HTML elements, according to this (last comment), it is necessary to initialize this property in order to be able to receive keystrokes on <DIV> elements.
clkey function performs proper mapping of keystrokes to simulate mouse clicks on buttons. It extracts the value of keyCode property from the event received, and remaps certain codes, finally passing a character to the continuation.
CalcST defines the internal state of the calculator. It includes the accumulator (where the user input goes), the stack (a list of numbers), and the reset flag which helps update the display properly when entry of a new number begins.
Internal logic of the calculator (
calc) is pretty straighforward: numbers typed in are pushed down the stack when "^" button is clicked, or the "up arrow" key is pressed. Operation buttons/keys perform binary operations between the accumulator and the topmost stack element. Finally copy of the result is pushed back onto the stack, so next number typed in may be used for the next computation.
btxt list of characters defines what shows on buttons.
The button creation function (
cbtn) defines a <DIV> element nesting a <BUTTON> element. Each button has an Activator based on
evtBCastA that maps each click to the character showing on the button. Note the float: left style and percentage width defined for <DIV>s: no matrix layout logic is necessary; buttons fill the <DIV> with borders with correct placement.