【发布时间】:2014-05-17 14:12:28
【问题描述】:
我正在尝试计算如何计算以下内容。
给定一个根值,找出以该值的最后一个字符开头的所有值。显然,如果路径中已经使用了元素,则不能重复。找到最大深度(最长路线)
例如使用种子 "sip" 和文字:
t1 = ["sour","piss","rune","profit","today","rat"]
我们会看到最大路径是 5。
siP 1 ---
| |
| |
pisS 2 profiT 2
| |
| |
| todaY 3
|
souR 3 ---
| |
| |
runE 4 raT 4
|
|
todaY 5
我认为我在以下方面处于正确的轨道 - 但我无法弄清楚如何实际递归调用它。
type Depth = Int
type History = Set.Set String
type AllVals = Set.Set String
type NodeVal = Char
data Tree a h d = Empty | Node a h d [Tree a h d] deriving (Show, Read, Eq, Ord)
singleton :: String -> History -> Depth -> Tree NodeVal History Depth
singleton x parentSet depth = Node (last x) (Set.insert x parentSet) (depth + 1) [Empty]
makePaths :: AllVals -> Tree NodeVal History Depth -> [Tree NodeVal History Depth]
makePaths valSet (Node v histSet depth trees) = newPaths
where paths = Set.toList $ findPaths valSet v histSet
newPaths = fmap (\x -> singleton x histSet depth) paths
findPaths :: AllVals -> NodeVal -> History -> History
findPaths valSet v histSet = Set.difference possible histSet
where possible = Set.filter (\x -> head x == v) valSet
所以...
setOfAll = Set.fromList xs
tree = singleton "sip" (Set.empty) 0
Node 'p' (fromList ["sip"]) 1 [Empty]
makePaths setOfAll tree
给予:
[Node 's' (fromList ["piss","sip"]) 2 [Empty],Node 't' (fromList ["profit","sip"]) 2 [Empty]]
但现在我不知道如何继续。
【问题讨论】:
标签: haskell recursion custom-data-type