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.
Some examples for the usage of the command-line program (using bash):
Also, preceding exclamation marks mean that the following number is imaginary, that is, you have to multiply it with i. E.g.:
hascal _1 ^ 0.5!1.0
And as you can see, negative numbers are preceded by an underscore.
Although hascal itself doesn't understand brackets, you can use your shell to get that functionality, like this (using bash):
hascal e ^ $(hascal i*pi)-1.0
Speaking of shells, you should consider that your shell might extend an asterisk (*) to the files at the current directory, like this:
echo *_darcs dist hascal.cabal Hascal.hs LICENSE Main.hs README.org Setup.hs
That's why this might not work:
hascal 1 * 2Error. :(
But you could do this instead:
Or, you could do:
Yeah, that's pretty much it. Hascal is really minimalistic. And I'm not planning to extend it much.
- operators :: RealFloat t => [(Char, Complex t -> Complex t -> Complex t)]
- eval :: (Read t, RealFloat t) => [(Char, Complex t -> Complex t -> Complex t)] -> String -> Either String (Complex t)
- hascal :: (Read t, RealFloat t) => String -> Either String (Complex t)
- prettyPrint :: (Show t, RealFloat t) => Complex t -> String
operators is the default list of operators.
An operator consists of one character and a function.
- addition, represented by
- subtraction, represented by
- multiplication, represented by
- 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)
|:: (Read t, RealFloat t)|
|=> [(Char, Complex t -> Complex t -> Complex t)]|
list of operators
string containing term
|-> Either String (Complex t)|
just result, or nothing
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.