Search Results

Search found 1 results on 1 pages for 'garulfo'.

Page 1/1 | 1 

  • Haskell newbie on types

    - by garulfo
    I'm completely new to Haskell (and more generally to functional programming), so forgive me if this is really basic stuff. To get more than a taste, I try to implement in Haskell some algorithmic stuff I'm working on. I have a simple module Interval that implements intervals on the line. It contains the type data Interval t = Interval t t the helper function makeInterval :: (Ord t) => t -> t -> Interval t makeInterval l r | l <= r = Interval l r | otherwise = error "bad interval" and some utility functions about intervals. Here, my interest lies in multidimensional intervals (d-intervals), those objects that are composed of d intervals. I want to separately consider d-intervals that are the union of d disjoint intervals on the line (multiple interval) from those that are the union of d interval on d separate lines (track interval). With distinct algorithmic treatments in mind, I think it would be nice to have two distinct types (even if both are lists of intervals here) such as import qualified Interval as I -- Multilple interval newtype MInterval t = MInterval [I.Interval t] -- Track interval newtype TInterval t = TInterval [I.Interval t] to allow for distinct sanity checks, e.g. makeMInterval :: (Ord t) => [I.Interval t] -> MInterval t makeMInterval is = if foldr (&&) True [I.precedes i i' | (i, i') <- zip is (tail is)] then (MInterval is) else error "bad multiple interval" makeTInterval :: (Ord t) => [I.Interval t] -> TInterval t makeTInterval = TInterval I now get to the point, at last! But some functions are naturally concerned with both multiple intervals and track intervals. For example, a function order would return the number of intervals in a multiple interval or a track interval. What can I do? Adding -- Dimensional interval data DInterval t = MIntervalStuff (MInterval t) | TIntervalStuff (TInterval t) does not help much, since, if I understand well (correct me if I'm wrong), I would have to write order :: DInterval t -> Int order (MIntervalStuff (MInterval is)) = length is order (TIntervalStuff (TInterval is)) = length is and call order as order (MIntervalStuff is) or order (TIntervalStuff is) when is is a MInterval or a TInterval. Not that great, it looks odd. Neither I want to duplicate the function (I have many functions that are concerned with both multiple and track intevals, and some other d-interval definitions such as equal length multiple and track intervals). I'm left with the feeling that I'm completely wrong and have missed some important point about types in Haskell (and/or can't forget enough here about OO programming). So, quite a newbie question, what would be the best way in Haskell to deal with such a situation? Do I have to forget about introducing MInterval and TInterval and go with one type only? Thanks a lot for your help, Garulfo

    Read the article

1