Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. --Function I working I am trying to find the shortest path
  2.  
  3. --I have this function
  4.  
  5. shortestPath graph from to = shortest (allPathsTo (reachableFrom graph from) to)
  6.  
  7. --I have this other function
  8.  
  9. shortest :: [[a]] -> Maybe [a]
  10. shortest xss
  11. | null xss = Nothing
  12. | otherwise = Just (minimumBy (comparing length) xss)
  13.  
  14.  
  15. extendedRecursionEngine termCond termFn reduceFn mapFn wayAheadFn x =
  16. recursiveFn x
  17. where recursiveFn y
  18. | termCond y = termFn y
  19. | otherwise = reduceFn (mapFn y) (map recursiveFn (wayAheadFn y))
  20.  
  21.  
  22. extendedRecursionEngine ::
  23. (a -> Bool) -- termCond
  24. -> (a -> b) -- termFn
  25. -> (t -> [b] -> b) -- reduceFn
  26. -> (a -> t) -- mapFn
  27. -> (a -> [a]) -- wayAheadFn
  28. -> a -- input
  29. -> b -- output
  30.  
  31. For our application the type a is Tree;
  32. b is [Path], i.e., a list of Paths;
  33. t is graph node, which is the value component of a Tree Node.
  34.  
  35. allPathsTo tree goal =
  36. extendedRecursionEngine
  37. -- termCond
  38. -- Terminate if there are no children or if the goal is one of the children.
  39. (\(Node _ children) -> null children || goal `elem` children )
  40.  
  41. -- termFn
  42. -- If there are no children, there are no paths.
  43. -- If we reached the goal create a list of paths. So far there is only one path.
  44. -- It is the path consisting of one Link from parent to goal.
  45. (\ (Node parent children) -> if null children then [] else [[(parent, goal)]])
  46. -- ReduceFn
  47. -- parent is a graph node.
  48. -- Since each child returns a list of paths, pathss is a list of lists of paths: [[Path]]
  49. -- For each path in the list of lists of paths, put the Link from parent
  50. -- to its start node in front as the first link.
  51. (\ parent pathss -> concatMap (\path -> parent : path) pathss) --- Error Message
  52. -- MapFn
  53. (\ (Node parent _) -> parent) -- Keep the parent for the reduceFn.
  54. -- wayAheadFn
  55. (\ (Node _ children) -> children) -- Pass along the list of children for the recursive calls.
  56. tree
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement