Tip: GHCi as a better bc

Posted on September 19, 2014

I use it for ages myself, and I thought I’d share:

$ cabal install statistics
$ cat .ghc/ghci.conf
import Control.Applicative
import Control.Monad
import Control.Monad.Instances
import Data.Monoid
import Data.Ratio -- approxRational
import Data.Vector
import Statistics.Sample

default (Double)

$ ghci <<< "1/3"
0.3333333333333333

$ ghci <<< "mean $ fromList [1e12,2e1,3e4]"
3.3333334334e11

Be cautious about “default (Double)”, though. It kind of creates a global state, which is not so fun to debug:

$ echo > empty.hs

$ ghci
λ= import Numeric.Matrix
λ= inv $ fromList [[1,1],[1,2]]
Just 2.0 -1.0
-1.0 1.0

λ= :l empty.hs

λ= inv $ fromList [[1,1],[1,2]]
Nothing