. , , makePaths findPaths, findPaths, makePaths makePaths findPaths . : -, , -, Set s.
.
. , n- , .
data Tree a = Empty | Node a [Tree a] deriving (Show, Read, Eq, Ord)
, Tree Tree
type OldTree a h d = Tree (a, h, d)
, - , String, :
makeTree :: String -> [String] -> Tree String
- , - , - . . , , , :
makeTree seed vals = Node seed children where
children = ...
. , , , vals . , " ". -
selectEach :: [a] -> [(a, [a])]
, (c, extras) , elem (c, extras) (selectEach lst) c:extras , lst, . -, ,
selectEach :: [a] -> [([a], a, [a])]
, (before, here, after) - , elem (before, here, after) (selectEach lst), lst == reverse before ++ [here] ++ after. .
selectEach [] = []
selectEach (a:as) = go ([], a, as) where
go (before, here, []) = [(before, here, [])]
go (before, here, after@(a:as)) = (before, here, after) : go (here:before, a, as)
> selectEach "foo"
[("",'f',"oo"),("f",'o',"o"),("of",'o',"")]
, .
makeTree seed vals = Node seed children where
children = map (\(before, here, after) -> makeTree here (before ++ after))
(selectEach vals)
.
makeTree "sip" ["sour","piss","rune","profit","today","rat"]
1957 8, . , , , . , .
goodTree :: String -> Tree String -> Bool
, "", . , node , , .
goodTree [] _ = False
goodTree seed Empty = False
goodTree seed (Node "" _) = False
goodTree seed (Node (h:_) _) = last seed == h
makeTree seed vals = Node seed children where
children =
filter goodTree
$ map (\(before, here, after) -> makeTree here (before ++ after))
$ selectEach
$ vals
!
> makeTree "sip" ["sour","piss","rune","profit","today","rat"]
Node "sip"
[ Node "piss" [ Node "sour" [ Node "rune" []
, Node "rat" [ Node "today" [] ]
]
]
, Node "profit" [ Node "today" [] ]
]
:
selectEach :: [a] -> [([a], a, [a])]
selectEach [] = []
selectEach (a:as) = go ([], a, as) where
go (before, here, []) = [(before, here, [])]
go (before, here, after@(a:as)) = (before, here, after) : go (here:before, a, as)
data Tree a = Empty | Node a [Tree a] deriving Show
goodTree :: Eq a => [a] -> Tree [a] -> Bool
goodTree [] _ = False
goodTree seed Empty = False
goodTree seed (Node [] _) = False
goodTree seed (Node (h:_) _) = last seed == h
makeTree :: Eq a => [a] -> [[a]] -> Tree [a]
makeTree seed vals = Node seed children where
children =
filter (goodTree seed)
$ map (\(before, here, after) -> makeTree here (before ++ after))
$ selectEach
$ vals
, selectEach , , makeTree Reader. , , .