Guest User

Untitled

a guest
Jan 17th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module PriorityQueue where
  2. import qualified Data.List as L
  3.  
  4. type Point = (Int, Int)
  5.  
  6. data Node = Node { point :: Point
  7.                   ,parent :: Maybe Node
  8.                   ,g :: Double
  9.                   ,h :: Double
  10.                  } deriving (Show)
  11.  
  12. instance Eq Node where
  13.   -- (==) = on (==) (g + h) -- miks ei toimi
  14.   (Node a1 parent1 g1 h1) == (Node a2 parent2 g2 h2) = (g1 + h1) == (g2 + h2)
  15.  
  16. instance Ord Node where
  17.   -- (>=) = on (>=) (g + h) -- miks ei toimi
  18.   (Node a1 parent1 g1 h1) `compare` (Node a2 parent2 g2 h2) = (g1 + h1) `compare` (g2 + h2)
  19.  
  20. priority :: Node -> Double
  21. priority (Node a parent g h) = g + h
  22.  
  23. getHighestPrioElem :: [Node] -> Maybe Node
  24. getHighestPrioElem [] = Nothing
  25. getHighestPrioElem xs = Just $ head (L.sort xs)
  26.  
  27. popHighestPrioElem :: [Node] -> Maybe [Node]
  28. popHighestPrioElem [] = Nothing
  29. popHighestPrioElem xs = Just $ tail (L.sort xs)
  30.  
  31. insertWithPriority :: Node -> [Node] -> [Node]
  32. insertWithPriority node xs
  33.  = L.sort (node:xs)
Add Comment
Please, Sign In to add comment