Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. module Homework7 (main) where
  2.  
  3. import Data.List(minimumBy)
  4. import Data.Ord(comparing)
  5.  
  6. --data Tree a = Leaf | Node (Tree a) a (Tree a)
  7.  
  8.  
  9. -- Nodes are not declared explicitly. A value of type a is a node.
  10. -- The nodes are linked by Links: (node_a, node_b)
  11. type Link a = (a, a)
  12. data (Eq a, Show a) =>
  13. Graph a = Graph {nodes :: [a], links :: [Link a]}
  14. deriving (Show, Eq)
  15. -- A Graph is a collection of values (of type a) with Links joining them.
  16.  
  17. exampleGraph =
  18. let node1 = 1
  19. node2 = 2
  20. node3 = 3
  21. node4 = 4
  22. node5 = 5
  23. node6 = 6
  24. in Graph [node1, node2, node3, node4, node5, node6]
  25. [ (node1, node2)
  26. , (node2, node3)
  27. , (node2, node4)
  28. , (node3, node5)
  29. , (node5, node2)
  30. , (node4, node1)
  31. , (node5, node6)
  32. ]
  33. data Tree a = EmptyTree | Node a [Tree a] deriving (Show, Read, Eq)
  34.  
  35. reachableFrom :: (Show a, Eq a) => Graph a -> a -> Tree a
  36.  
  37. extendedRecursionEngine termCond termFn reduceFn mapFn wayAheadFn x =
  38. recursiveFn x
  39. where recursiveFn y
  40. | termCond y = termFn y
  41. | otherwise = reduceFn (mapFn y) (map recursiveFn (wayAheadFn y))
  42.  
  43. linksFrom :: (Eq a, Show a) => Graph a -> a -> [a]
  44. linksFrom graph node = [n2 | (n1, n2) <- links graph, n1 == node]
  45.  
  46. reachableFrom graph startNode =
  47. extendedRecursionEngine
  48. (\ (node, expanded) -> node `elem` expanded ) -- Stop if we have already expanded this node.
  49. (\ (node, _) -> Node node []) -- If we are done, construct a Tree Node with no descendents.
  50. Node -- Build a Tree Node from a graph node and the returned subtrees.
  51. (\ (node, _) -> node) -- Save the graph node for the reduceFn.
  52. (\(node, expanded) -> [ (node', node:expanded) | node' <- linksFrom graph node ]) -- Construct a list of nodes that can be reached in one step.
  53. -- Also add the current node to expanded.
  54. (startNode, []) -- Start at the start node. expanded is initially empty.
  55.  
  56.  
  57. --shortestPath :: (Eq a, Show a) => Graph a -> a -> a -> Maybe [(a, a)]
  58. -- Recall: Path = [Link] and Link = (a, a). So:
  59. --shortestPath :: (Eq a, Show a) => Graph a -> a -> a -> Maybe (Path a)
  60.  
  61. --allPathsTo :: (Eq a) => Tree a -> a -> [[(a, a)]]
  62. -- Recall: Path = [Link] and Link = (a, a). So:
  63. allPathsTo :: (Eq a) => Tree a -> a -> [Path a]
  64. -- Return a list of Paths from the tree's root to the goal.
  65. -- If there are no paths, the list will be empty.
  66.  
  67. type Path a = [Link a]
  68.  
  69. shortestPath :: (Eq a, Show a) => Graph a -> a -> a -> Maybe (Path a)
  70.  
  71.  
  72. allPathsTo tree goal =
  73. extendedRecursionEngine
  74. -- termCond
  75. -- Terminate if there are no children or if the goal is one of the children.
  76. (\(Node _ children) -> null children || goal `elem` children )
  77.  
  78. -- termFn
  79. -- If there are no children, there are no paths.
  80. -- If we reached the goal create a list of paths. So far there is only one path.
  81. -- It is the path consisting of one Link from parent to goal.
  82. (\ (Node parent children) -> if null children then [] else [[(parent, goal)]])
  83. -- ReduceFn
  84. -- parent is a graph node.
  85. -- Since each child returns a list of paths, pathss is a list of lists of paths: [[Path]]
  86. -- For each path in the list of lists of paths, put the Link from parent
  87. -- to its start node in front as the first link.
  88. (\ parent pathss -> map (\path -> parent : path) pathss) --- Error Message
  89. -- MapFn
  90. (\ (Node parent _) -> parent) -- Keep the parent for the reduceFn.
  91. -- wayAheadFn
  92. (\ (Node _ children) -> children) -- Pass along the list of children for the recursive calls.
  93. tree
  94.  
  95.  
  96.  
  97. -- Return the shortest path in graph from the from node to the to node.
  98. -- Use Maybe/Just/Nothing to cover cases where there are no paths.
  99. shortestPath graph from to = shortest (allPathsTo (reachableFrom graph from) to)
  100.  
  101. -- If xss (a list of lists) is null, return Nothing; otherwise return Just <the shortest list>
  102.  
  103.  
  104. -- If xss (a list of lists) is null, return Nothing; otherwise return Just
  105.  
  106. shortest :: [[a]] -> Maybe [a]
  107. shortest xss
  108. | null xss = Nothing
  109. | otherwise = Just (minimumBy (comparing length) xss)
  110. -- For a cool way to sort, look up "sortBy" in the modules chapter.
  111.  
  112. extendedRecursionEngine ::
  113. (a -> Bool) -- termCond
  114. -> (a -> b) -- termFn
  115. -> (t -> [b] -> b) -- reduceFn
  116. -> (a -> t) -- mapFn
  117. -> (a -> [a]) -- wayAheadFn
  118. -> a -- input
  119. -> b -- output
  120.  
  121. For our application the type a is Tree;
  122. b is [Path], i.e., a list of Paths;
  123. t is graph node, which is the value component of a Tree Node.
  124.  
  125.  
  126.  
  127.  
  128. Error Message
  129.  
  130.  
  131. Homework7.hs:91:55:
  132.  
  133. Couldn't match expected type `(t, a)'
  134.  
  135. against inferred type `[(t, a)]'
  136.  
  137. Expected type: [[(t, a)]]
  138.  
  139. Inferred type: [[[(t, a)]]]
  140.  
  141. In the second argument of `map', namely `pathss'
  142.  
  143. In the expression: map (\ path -> parent : path) pathss
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement