-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex4.hs
66 lines (48 loc) · 2.04 KB
/
ex4.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import Probability
import Cp
import List
import Data.List
data Stop = S0 | S1 | S2 | S3 | S4 | S5 deriving (Show, Eq, Ord, Enum)
-- data Stop Int = S Int deriving (Show, Eq, Ord, Enum)
type Segment = (Stop, Stop)
type Delay = Int
type Dados = [(Segment, Delay)]
type Db = [(Segment, Dist Delay)]
-- funções fornecidas
dados :: [(Segment, Delay)]
dados = [((S0, S1), 0), ((S0, S1), 2), ((S0, S1), 0), ((S0, S1), 3), ((S0, S1), 3),
((S1, S2), 0), ((S1, S2), 2), ((S1, S2), 1), ((S1, S2), 1), ((S1, S2), 4),
((S2, S3), 2), ((S2, S3), 2), ((S2, S3), 4), ((S2, S3), 0), ((S2, S3), 5),
((S3, S4), 2), ((S3, S4), 3), ((S3, S4), 5), ((S3, S4), 2), ((S3, S4), 0),
((S4, S5), 0), ((S4, S5), 5), ((S4, S5), 0), ((S4, S5), 7), ((S4, S5), -1)]
mkf :: Eq a => [(a, b)] -> a -> Maybe b
mkf = flip Prelude.lookup
instantaneous :: Dist Delay
instantaneous = D [(0, 1)]
-- mkdist
msetplus :: Eq a => [a] -> ([(a, Int)],Int)
msetplus [] = ([],0)
msetplus (h:t) = (((h,c):(x)),y+c)
where (x,y) = msetplus rest
(c,rest) = (1+ length (filter (h ==) t) , (filter (h /=)) t)
relativeFrequence :: (Eq b) => [b] -> [(b, Float)]
relativeFrequence l = map (id><(((/fromIntegral s).fromIntegral))) mset where (mset,s) = msetplus l
mkdist :: Eq a => [a] -> Dist a
mkdist = mkD . relativeFrequence
--
db :: [(Segment, Dist Delay)]
db = map (split (p1 . head) ((mkdist .(map p2)))) . groupBy (\x y -> p1 x == p1 y) $ dados
delay :: Segment -> Dist Delay
delay = (either (const instantaneous) id).outMaybe.mkf db
ana_devide :: (Stop ,Stop) -> [Segment]
ana_devide = anaList devide
cata_conquer :: [Segment] -> Dist Delay
cata_conquer = cataList conquer
devide :: (Eq b, Enum b,Ord b) => (b, b) -> Either () ((b, b), (b, b))
devide (s,final) | s >= final = i1 ()
| otherwise = i2 ((s,succ s),(succ s,final))
conquer :: Either a (Segment, Dist Delay) -> Dist Delay
conquer = (either (const instantaneous) aux)
where aux = uncurry (joinWith (+) . delay )
pdelay :: Stop -> Stop -> Dist Delay
pdelay = curry (hyloList conquer devide)