test1 правильно производит следующую структуру из строки «ABCDEF»:Как параметризовать константу (в этой конкретной рекурсивной функции)?
(a,(1,[0])) -- type 'a' occur 1 time in position 0
(b,(1,[1])) -- type 'b' occur 1 time in position 1
(c,(1,[2]))
(d,(1,[3]))
(e,(1,[4]))
(f*,(1,[5])) -- type 'f' is the last of the list
Но этот результат зависит от числа 6, то есть длина очень определенного класса строки, недопустима для общего случая.
Так, если строка в test1 вместо этого «а» результат является неправильным:
(a,(1,[0]))
(b,(1,[7]))
(c*,(1,[8]))
Если строка в test1 вместо этого «ABCDEFGH» результат тоже неправильно:
(a,(1,[0]))
(b,(1,[2])) -- Should be [1]
(c,(1,[3])) -- Should be [2]
(d,(1,[4])) -- ...
(e,(1,[5]))
(f,(1,[6]))
(g,(1,[7]))
(h*,(1,[8]))
В addTrieWithCounter я не могу заменить эту константу (6) с parameterized function on the length of the word
.
КОНТЕКСТ этой функции. AddTrieWithCounter будет помещен в специальный «цикл», такой как «al alts» becames: addTrieWithCounter ... «al» 0 -> «drop the space» -> addTrieWithCounter ... «alts» 3. Таким образом, вхождения будут выравнивается с исходной строкой.
-- analyzing "all alts" should be obtained this result.
(a,(2,[4,0])) -- type 'a' occur 2 times in positions 3 and 0 (reversed order)
(l,(2,[5,1])) -- type 'l' (of seq "al") occur 2 times in positions 4 and 1 (reversed order)
(l*,(1,[2])) -- type 'l' (of seq "all") occur 1 time in positions 2
(t,(1,[6])) -- type 't' (of seq "alt") occur 1 time in positions 6
(s*,(1,[7])) -- type 's' (of seq "alts") occur 1 time in positions 7
Это будет тривиальная вещь, но я понятия не имею.
Заранее благодарим за ваши предложения.
import qualified Data.Map as M
import Text.PrettyPrint as TP
import Data.Either (either)
data Trie a b = Nil | Trie (M.Map (Either a a) (b, Trie a b)) deriving Show
-- (Just a note: Trie will be a Monoid's instance. So with "Either" it is possible to distinguish the following cases: "all" and "alliance")
-- add an element to a Trie
addTrieWithCounter
:: Ord a =>
(Trie a (Int, [t1]), Int)
-> ((Int, [t1]) -> Int -> (Int, [t1]))
-> [a]
-> (Trie a (Int, [t1]), Int)
addTrieWithCounter (t,st) f [] = (t,st)
addTrieWithCounter (Nil,st) f xs = addTrieWithCounter (Trie M.empty, st) f xs
addTrieWithCounter (Trie m,st) f [x] =
(Trie $ M.insertWith (\(c,_) _ -> (f c st,Nil)) (Left x) (f (0,[]) st,Nil) m,st + 1)
addTrieWithCounter (Trie m, st) f (x:xs) =
case M.lookup (Right x) m of -- !!!!! PROBLEM IN THE FOLLOWING LINE !!!!!
Nothing -> let (t',st') = addTrieWithCounter (Nil, 6 - length xs) f xs
in (Trie $ M.insert (Right x) (f (0,[]) st,t') m,st + 1)
Just (c,t) -> let (t',st') = addTrieWithCounter (t,st) f xs -- TO CHANGE
in (Trie $ M.insert (Right x) (f c st',t') m,st')
showTrieS f (t,_) = showTrie f t
showTrie :: Show a => (Either t t -> String) -> Trie t a -> Doc
showTrie _ Nil = empty
showTrie f (Trie m)
| M.null m = empty
| otherwise =
vcat $
do (k,(count,t)) <- M.assocs m
return $
vcat [ lparen TP.<> text (f k) TP.<> comma TP.<> (text . show $ count) TP.<> rparen
, nest 4 (showTrie f t)
]
test1 = showTrieS f1 t
where
f1 = (either (:"*") (:""))
t = addTrieWithCounter (Trie M.empty,0) f2 "abcdef"
f2 (cr,poss) st = ((cr + 1),(st : poss))
Может список, связанный с узлом в синтаксическом дереве когда-либо иметь более чем одно число - то, что это означало бы, если отображение синтаксического дерева показало '(с, (1, [2,3,4])) '? Может ли это когда-либо показать '(c, (1, [2,2,2,3]))? Разве это не число в списке, просто глубина узла в trie? – ErikR
Это может быть так. Но addTrieWithCounter будет размещен в специальном «цикле», так что «все альянсы» стали: addTrieWithCounter ... «all» 0 -> «drop the spaces» -> addTrieWithCounter ... «альянсы» 3. Таким образом, вхождения будут выравнивается с исходной строкой. –