[Haskell-beginners] Need help to write join on my Temporals

martin martin.drautzburg at web.de
Sun Apr 12 09:49:13 UTC 2015


So I have a solution now, it passes my tests but it is ugly. While I am unnesting I sometimes have to add the default as
a new change and sometimes add the last old change with its time advanced.

The problem is, I don't really know the semantics of a Temporal Temporal, but I know the semantics of <*> and that <*>
must be the same as `ap`.

Any comments will be very welcome.

-- cBetween :: Time -> Time -> [Change a] -> [Change a]
-- cAfter   :: Time -> [Change a] -> [Change a]
-- cBefore  :: Time -> [Change a] -> [Change a]


tJoin :: Temporal (Temporal a) -> Temporal a
tJoin (Temporal tdef []) = tdef
tJoin tp@(Temporal tdef ctps)
        | null cs'  =  Temporal (td tdef) (tj ctps)
        | otherwise =  Temporal (td tdef) (cs' ++ tj' ctps)
        where
            cs  = tc tdef
            cs' = cBefore (ct $ head ctps) cs

tj, tj' :: [Change (Temporal a)] -> [Change a]

-- before first change was found
tj  ((Chg t (Temporal d [])):[])  =  [Chg t d]
tj  ((Chg t (Temporal d cs)):[])  =  preDef t d cs (cAfter t cs)

tj  ((Chg t (Temporal d [])):cts) =  (Chg t d) : (tj cts)
tj  ((Chg t (Temporal d cs)):cts)
        | null cs'  =  preDef t d cs (tj cts)
        | otherwise =  preDef t d cs cs' ++ (tj' cts)
          where
              cs' = cBetween t (ct $ head cts) cs

-- after first change was found
tj' ((Chg t (Temporal d cs)):[]) =  preC0 t cs (cAfter t cs)

tj' ((Chg t (Temporal d cs)):cts) =  preC0 t cs cs' ++ (tj' cts)
          where
              cs' =  cBetween t (ct $ head cts) cs

-- prepend first change if required
preC0 t cs cs'
        | null bef   = cs'
        | tx == t    = cs'
        | otherwise  = (Chg t vx) : cs'
        where
            bef          = cBefore' t cs
            (Chg tx vx)  = last bef

-- prepend default as new change
preDef t d cs cs'
        | null cs   = cs'
        | t == tx   = cs'
        | otherwise = (Chg t d) : cs'
        where
            (Chg tx vx) = head cs





Am 04/10/2015 um 07:02 PM schrieb martin:

> type Time = Integer
> data Change a = Chg {
>             ct :: Time, -- "change time"
>             cv :: a     -- "change value"
>         } deriving (Eq,Show)
> 
> data Temporal a = Temporal {
>     td :: a,         -- "temporal default"
>     tc :: [Change a] -- "temporal changes"
> } deriving (Eq, Show)



More information about the Beginners mailing list