[英]How does the path function in Data.Graph work in Haskell?
我已经阅读了可以在路径 function 上找到的文档(对于有向图)。 搜索引擎中没有太多内容。
我发现的所有内容基本上都归结为这个链接。
它真正说的是路径 function 所做的(我已经知道),以及 output 的示例。 我想弄清楚的是路径 function 是如何工作的,或者它是如何手动实现的。 到目前为止,我已经走了这么远,但现在卡住了(或者可能完全走错了路,我不能确定):
member _ [] = False
member n (x:xs)
| x == n = True
| otherwise = membre n xs
getX :: (a, b) -> a
getX (x,_) = x
getY :: (a, b) -> b
getY (_,y) = y
getAllY :: [(a , b)] -> [b]
getAllY [] = []
getAllY (x:xs) = (getY x):(getAllY xs)
fltrX [] _ = []
fltrX (x:xs) n
| (getX x) == n = x:(fltrX xs n)
| otherwise = fltrX xs n
fltrY [] _ = []
fltrY (x:xs) n
| (getY x) == n = x:(fltrY xs n)
| otherwise = fltrY xs n
path' :: [(a , a)] -> a -> a -> Bool
path' [] _ _ = False
path' (xs) n m
| (member m (getAllY (fltrX xs n))) = True
| otherwise = *recursive statement giving me a headache*
但我无法看到如何正确地进行递归。 我想我只是盯着它太久了,不能再清楚地看到整个问题。 逻辑应该是这样的:
任何人都可以在不为我输入代码的情况下为我指出正确的方向(双关语)吗?
谢谢!
感谢评论者提供帮助我编写以下解决方案的资源:
member _ [] = False
member n (x:xs)
| x == n = True
| otherwise = member n xs
unique [] = []
unique (x:xs)
| member x xs == True = unique xs
| otherwise = x:(unique xs)
findAllEndsForX :: [(Int , Int)] -> Int -> [Int]
findAllEndsForX [] _ = []
findAllEndsForX ((a,b):xs) y
| xs == [] = a:[b]
| a == y = a:(findAllEndsForX xs b)
| otherwise = (findAllEndsForX xs b)
accessible :: [(Int , Int)] -> Int -> [Int]
accessible [] _ = []
accessible xs n = unique (findAllEndsForX xs n)
path' :: [(Int , Int)] -> Int -> Int -> Bool
path' [] _ _ = False
path' xs v w = member w (accessible xs v)
我的最终目标是模仿路径功能的功能,而不一定是实现。 我想在不使用任何预定义函数的情况下完成这项工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.