Lazy Arithmetic in Haskell
24/03/2010We don’t usually give much thought to Numeric data types beyond whether we want to work with integers or decimal numbers. And that is a shame! In this post I’ll look at how we can actually do arithmetic operations lazily, and in the process hopefully reveal a bit about haskell’s numeric classes.
> module LazyArithmetic
> where
We will be using Lennart Augustsson’s numbers library which can be installed from hackage with cabal-install:
$> cabal install numbers
> import Data.Number.Natural
> import Data.List(genericLength)
Consider two functions: the Prelude function sum and genericLength from the List library:
genericLength :: (Num i) => [b] -> i
sum :: (Num a) => [a] -> a
…two simple functions that have the potential to be very expensive, depending on the length of the list and the values of the elements.
Read the rest of this article »