hascal-1.1: a minimal, extensible and precise calculator

Safe HaskellSafe-Infered




Hascal is both a simple but extendable calculator library for Haskell and a command-line program using it.

Also, its source code is a nice example for a minimalistic Haskell project.




operators :: [(Char, CReal -> CReal -> CReal)]Source

operators is the default list of operators.

An operator consists of one character and a function with of type CReal -> CReal -> CReal.

operators includes:

  • addition, represented by '+'
  • subtraction, represented by '-'
  • multiplication, represented by 'c'
  • division, represented by '/'
  • exponentiation, represented by '^'
  • logarithming (with flipped arguments, see below), represented by '?'

such that these laws are held:

 (a - b == c) == (a == b + c)
 (a / b == c) == (a == b * c)
 (a ? b == c) == (a == b ^ c)


eval :: [(Char, CReal -> CReal -> CReal)] -> String -> CRealSource

eval gets a list of operators and a string containing a mathematical expression/term which only uses those operators listed in the first argument, and returns the result of that term.

hascal :: String -> CRealSource

hascal is the default evaluator:

 hascal = eval operators