[Haskell-cafe] practice problems?

Paul Johnson paul at cogito.org.uk
Sun Sep 3 12:32:31 EDT 2006

"Brian Hulley" <brianh at metamilk.com> wrote:

    What about a library for interval arithmetic [1]?

    I'd imagine it could start something like:

    data Interval a = Interval !a !a deriving (Eq, Show)

    instance Num a => Num (Interval a) where

    Interval llow lhigh + Interval rlow rhigh = Interval (min llow rlow)
    (max lhigh rhigh)

    The Interval type would probably need to explicitly represent
    several kinds
    of intervals eg (-infinity, x] etc and there are some tricky issues
    what to do with the operations whose result sometimes needs to be
    represented by more than one interval to be useful such as division
    by an
    interval containing zero eg you might want to use a list of
    intervals to
    deal with these cases:

    instance Num a => [Interval a] where

    [Interval 5 5] / [Interval -1 1] = [FromNegInfinityTo -5,
    ToPosInfinityFrom 5]

Take a look at my Ranged Sets library at



More information about the Haskell-Cafe mailing list