-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinSetPath.hs
More file actions
27 lines (19 loc) · 900 Bytes
/
MinSetPath.hs
File metadata and controls
27 lines (19 loc) · 900 Bytes
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
module MinSetPath where
minsetpath :: [(Int,Int)] -> Int
minsetpath list = minpath 0 list
maxofmins :: (Int,Int) -> (Int,Int) -> Int
maxofmins x y = max (fst x) (fst y)
minofmaxs :: (Int,Int) -> (Int,Int) -> Int
minofmaxs x y = min (snd x) (snd y)
overlaps :: (Int,Int) -> (Int,Int) -> Bool
x `overlaps` y = maxofmins x y <= minofmaxs x y
intersection :: (Int,Int) -> (Int,Int) -> (Int,Int)
intersection x y = ((maxofmins x y), (minofmaxs x y))
dist :: (Int,Int) -> (Int,Int) -> Int
dist x y = (maxofmins x y) - (minofmaxs x y)
minpath :: Int -> [(Int,Int)] -> Int
minpath t [] = t
minpath t [x] = t
minpath t (x:y:xs) | x `overlaps` y = minpath t (z:xs) where z = intersection x y
minpath t (x:y:xs) | snd x < fst y = minpath (t+d) (z:xs) where d = dist x y; z = (fst y, fst y)
minpath t (x:y:xs) | otherwise = minpath (t+d) (z:xs) where d = dist x y; z = (snd y, snd y)