Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Function I working I am trying to find the shortest path
- --I have this function
- shortestPath graph from to = shortest (allPathsTo (reachableFrom graph from) to)
- --I have this other function
- shortest :: [[a]] -> Maybe [a]
- shortest xss
- | null xss = Nothing
- | otherwise = Just (minimumBy (comparing length) xss)
- extendedRecursionEngine termCond termFn reduceFn mapFn wayAheadFn x =
- recursiveFn x
- where recursiveFn y
- | termCond y = termFn y
- | otherwise = reduceFn (mapFn y) (map recursiveFn (wayAheadFn y))
- extendedRecursionEngine ::
- (a -> Bool) -- termCond
- -> (a -> b) -- termFn
- -> (t -> [b] -> b) -- reduceFn
- -> (a -> t) -- mapFn
- -> (a -> [a]) -- wayAheadFn
- -> a -- input
- -> b -- output
- For our application the type a is Tree;
- b is [Path], i.e., a list of Paths;
- t is graph node, which is the value component of a Tree Node.
- allPathsTo tree goal =
- extendedRecursionEngine
- -- termCond
- -- Terminate if there are no children or if the goal is one of the children.
- (\(Node _ children) -> null children || goal `elem` children )
- -- termFn
- -- If there are no children, there are no paths.
- -- If we reached the goal create a list of paths. So far there is only one path.
- -- It is the path consisting of one Link from parent to goal.
- (\ (Node parent children) -> if null children then [] else [[(parent, goal)]])
- -- ReduceFn
- -- parent is a graph node.
- -- Since each child returns a list of paths, pathss is a list of lists of paths: [[Path]]
- -- For each path in the list of lists of paths, put the Link from parent
- -- to its start node in front as the first link.
- (\ parent pathss -> concatMap (\path -> parent : path) pathss) --- Error Message
- -- MapFn
- (\ (Node parent _) -> parent) -- Keep the parent for the reduceFn.
- -- wayAheadFn
- (\ (Node _ children) -> children) -- Pass along the list of children for the recursive calls.
- tree
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement