[Fudgets Home Page]

A Simple Pocket Calculator


Using the ideas illustrated by the previous examples you can build a simple pocket calculator.

[Your browser doesn't support fupplets. Here is a window dump instead:]

[Window dump of the Up/Down/Reset Counter]

For simplicity, postfix notation is used, i.e., to compute 3+4 you enter 3 Ent 4 +.

Here is the source code:

import Fudgets

main = fudlogue (shellF "Pocket Calculator" calcF)

calcF = intDispF >==< mapstateF calc [0] >==< buttonsF

data Buttons = Plus | Minus | Times | Div | Enter | Digit Int   deriving (Eq)

buttonsF = placerF (matrixP 4) (
              listF [d 7, d 8, d 9,op Div,
                     d 4, d 5, d 6,op Times,
		     d 1, d 2, d 3,op Minus,
		     hole,d 0,ent, op Plus])
  where
    d n = (Digit n,buttonF (show n))
    ent = op Enter
    hole = (Enter,holeF)
    op o = (o,buttonF (opLabel o))
      where opLabel Plus = "+"
            opLabel Minus = "-"
	    opLabel Times  = "*"
	    opLabel Div = "/"
	    opLabel Enter = "Ent"

calc (n:s)   (Digit d,_) = new (n*10+d) s
calc s       (Enter,_)   = (0:s,[])
calc (y:x:s) (Plus,_)    = new (x+y) s
calc (y:x:s) (Minus,_)   = new (x-y) s
calc (y:x:s) (Times,_)   = new (x*y) s
calc (y:x:s) (Div,_)     = new (x `div` y) s
calc s       _           = (s,[])

new n s = (n:s,[n])

Things to note

The program structure is much the same as in the Up/Down/Reset counter.
  • Previous Example
  • Back to the Example list