Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 表示类型和事件:(如此)易于理解,(如此)难以编码_Haskell_Types_Linguistics - Fatal编程技术网

Haskell 表示类型和事件:(如此)易于理解,(如此)难以编码

Haskell 表示类型和事件:(如此)易于理解,(如此)难以编码,haskell,types,linguistics,Haskell,Types,Linguistics,通过示例对类型和事件进行简要介绍 例1abbacb a,b,c是这些类型 a发生2次b发生3次c发生1次 这可以更简洁地表示为[('a',2),('b',3),('c',1)](实际上,顺序并不重要) 例2abbacb ab,bb,ba,ac,cb是类型序列 每个序列只发生一次 这可以表示为[(“ab”,1),(“bb”,1),(“ba”,1),(“ac”,1),(“cb”,1)] 以下图形结构具有与前两个相同的信息内容: ('a',2) -- 'a' occurs 2 ti

通过示例对类型和事件进行简要介绍

例1<代码>abbacb

a
b
c
是这些类型

a
发生2次<代码>b发生3次<代码>c发生1次

这可以更简洁地表示为
[('a',2),('b',3),('c',1)]
(实际上,顺序并不重要)

例2<代码>abbacb

ab
bb
ba
ac
cb
是类型序列

每个序列只发生一次

这可以表示为
[(“ab”,1),(“bb”,1),(“ba”,1),(“ac”,1),(“cb”,1)]

以下图形结构具有与前两个相同的信息内容:

('a',2)          -- 'a'  occurs 2 times
     ('b',1)     -- "ab" occurs 1 times
     ('c',1)     -- "ac" occurs 1 times
('b',2)          -- 'b'  occurs 2 times
     ('a',1)     -- "ba" occurs 1 times
     ('b',1)     -- "bb" occurs 1 times
('c',1)          -- 'c'  occurs 1 times
     ('b',1)     -- "cb" occurs 1 times
在哈斯凯尔语中:
[('a',2),[('b',1),('c',1)],('b',2),[('a',1),('b',1)],('c',1),[('b',1)]

对于3个元素序列的出现:

('a',2)              -- 'a'   occurs 2 times
     ('b',1)         -- "ab"  occurs 1 times
          ('b',1)    -- "abb" occurs 1 times
     ('c',1)         -- "ac"  occurs 1 times
          ('b',1)    -- "acb" occurs 1 times
...
在哈斯克尔:

[
    (('a',2), [(('b',1),[('b',1)]),(('c',1),[('b',1)])]),
    (('b',2), [(('a',1),[('c',1)]),(('b',1),[('a',1)])])
]
使用类型
[((字符,整数),[((字符,整数),[(字符,整数)])]]

即使只考虑两个和三个元素的序列,图形表示的可理解性也比Haskell中的要大得多

此外,列表的效率不是很高,因此我使用了
Data.Map
库,因此使用了稍微不同的表示形式

以下示例基于Pi的数字。用小说的话可以得到有趣的结果

我的问题是

  • 专门用于这三种类型的序列的函数非常复杂。有可能大大简化它们吗

  • 我甚至无法想象如何将函数推广到任意长度的序列。有人知道怎么做吗

  • 使用以下数据类型递归应该更容易实现:

    data TuplesTypesOccurences a = L (M.Map a Int) | B  (M.Map a (Int,TuplesTypesOccurences a))
    
    但是,通过这种方式不会失去对
    Data.Map
    库中所有函数的访问权

    import qualified Data.Map as M
    import Data.List (sortBy)
    
    piDigits = "31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756"
    
    type TypesOccurrences a = M.Map a Int
    
    toTypeOccurrences :: Ord k => [k] -> TypesOccurrences k -> TypesOccurrences k  
    toTypeOccurrences [] mp = mp
    toTypeOccurrences (x:xs) mp = toTypeOccurrences xs $ M.insertWith (+) x 1 mp
    -- ex. toTypeOccurrences piDigits M.empty
    
    pprintTO :: Show a => TypesOccurrences a -> IO ()
    pprintTO = mapM_ putStrLn . map (\(xs,n) -> show xs ++ "  " ++ (show n)). sortBy (\x y -> compare (snd y) (snd x)) . M.toList
    -- ex. pprintTO . M.filter (> 22) . toTypeOccurrences piDigits $ M.empty
    
    type Seq2TypeOccurrences a = M.Map a (Int,TypesOccurrences a)
    
    toSQ2TO :: Ord a => [a] -> Seq2TypeOccurrences a -> Seq2TypeOccurrences a
    toSQ2TO []       mp = mp
    toSQ2TO [x]      mp = mp
    toSQ2TO (x:y:xs) mp = toSQ2TO (y:xs) $
      case M.lookup x mp of
        Nothing       -> M.insert x (1,M.singleton y 1) mp
        Just (_,mp2) -> case M.lookup y mp2 of
            Nothing -> M.update (\(n,mp2) -> Just (n+1,M.insert y 1 mp2)) x mp
            Just _  -> M.update (\(n,mp2) -> Just (n+1,M.update (\m -> Just (m+1)) y mp2)) x mp
    -- ex. toSQ2TO piDigits M.empty
    
    pprintSQ2TO :: Show a => Seq2TypeOccurrences a -> IO ()
    pprintSQ2TO = mapM_ putStrLn . map (\(x,(n,mp)) -> "(" ++ (show x) ++ "," ++ (show n) ++ ")\n\t" ++ (drop 2 . concatMap (("\n\t" ++) . show) . M.toList $ mp)) . M.toList
    -- ex. pprintSQ2TO (toSQ2TO piDigits  M.empty)
    
    greaterThanSQ2TO :: Ord a => Int -> Seq2TypeOccurrences a -> Seq2TypeOccurrences a
    greaterThanSQ2TO n =   M.filter (\(_,mp2) -> not . M.null $ mp2) . M.map (\(o,mp2) -> (o,M.filter (> n) mp2)) . M.filter (\(m,mp) -> m > n)
    -- ex. pprintSQ2TO . greaterThanSQ2TO 4 . toSQ2TO piDigits $ M.empty
    
    descSortSQ2TO :: Ord a => Seq2TypeOccurrences a -> [([a], Int)]
    descSortSQ2TO = sortBy (\xs ys -> compare (snd ys) (snd xs)) . concatMap (\(x,ys) -> zipWith (\x (y,n) -> ([x,y],n)) (repeat x) ys ) . map (\(x,(_,mp2)) -> (x,M.toList mp2)) . M.toList
    -- mapM_ print . descSortSQ2TO . greaterThanSQ2TO 4 . toSQ2TO piDigits $ M.empty
    
    unionSQ2TO :: Ord a => Seq2TypeOccurrences a -> Seq2TypeOccurrences a -> Seq2TypeOccurrences a
    unionSQ2TO = M.unionWith (\(n1,mp1) (n2,mp2) -> (n1+n2, M.unionWith (+) mp1 mp2))
    
    type Seq3TypeOccurrences a = M.Map a (Int,Seq2TypeOccurrences a)
    
    toSQ3TO :: Ord k => [k] -> Seq3TypeOccurrences k -> Seq3TypeOccurrences k
    toSQ3TO []    mp = mp
    toSQ3TO [x]   mp = mp
    toSQ3TO [x,y] mp = mp
    toSQ3TO (x:y:z:xs) mp = toSQ3TO (y:z:xs) $
      case M.lookup x mp of
        Nothing -> M.insert x (1,M.singleton y (1,M.singleton z 1)) mp
        Just (_,mp2) -> case M.lookup y mp2 of
           Nothing -> M.update (\(n,mp2) -> Just (n+1,M.insert y (1,M.singleton z 1) mp2)) x mp
           Just (m,kns3)  -> case M.lookup z kns3 of
                Nothing -> M.update (\(n,_) -> Just (n+1,M.update (\(m,mp3) -> Just (m+1,M.insert z 1    mp3)) y mp2)) x mp
                Just _  -> M.update (\(n,_) -> Just (n+1,M.update (\(m,mp3) -> Just (m+1,M.update (Just . (+1)) z mp3)) y mp2)) x mp
    -- ex. toSQ3TO piDigits M.empty
    
    pprint3 :: Show a => Seq3TypeOccurrences a -> IO ()
    pprint3 = mapM_ putStrLn . map (\(x,(n,mp)) -> "(" ++ (show x) ++ "," ++ (show n) ++ ")" ++ (concatMap (\(x2,(n2,mp2)) -> "\n\t(" ++ (show x2) ++ "," ++ (show n2) ++ ")" ++ (f mp2)) . M.toList $ mp)) . M.toList
      where
      f = concatMap (\(x,n) -> "\n\t\t(" ++ (show x) ++ "," ++ (show n) ++ ")") . M.toList
    -- pprint3 . toSQ3TO piDigits $ M.empty  
    
    pprint3B :: Show a => Seq3TypeOccurrences a -> IO ()
    pprint3B = mapM_ putStrLn . map (\(xs,n) -> show xs ++ "  " ++ (show n)) . concatMap (\(xs,mp) -> zipWith (\ys (z,n) -> (ys ++ [z],n)) (repeat xs) mp) . concatMap (\(x,mp) -> zipWith (\y (z,mp2) -> ([y,z],mp2)) (repeat x) mp) . map (\(x,(_,mp)) -> (x, map (\(y,(_,mp2)) -> (y, M.toList mp2)) $ M.toList mp)) . M.toList
    -- pprint3B . toSQ3TO piDigits $ M.empty 
    
    greaterThan3Q2TO :: Ord a => Int -> Seq3TypeOccurrences a ->  Seq3TypeOccurrences a
    greaterThan3Q2TO n = M.filter (\(_,mp) -> not . M.null $ mp)
      . M.map (\(m,mp) -> (m,M.filter (\(o,mp2) -> not . M.null $ mp2) mp))
      . M.map (\(m,mp) -> (m,M.map (\(o,mp2) -> (o,M.filter (>n) mp2)) mp))
      . M.filter (\(_,mp) -> not. M.null $ mp)
      . M.map (\(m,mp) -> (m,M.filter ((n <) . fst) mp))
      . M.filter (\(m,mp) -> m > n)
    -- ex. pprint3B . greaterThan3Q2TO 2 . toSQ3TO piDigits $ M.empty
    
    unionSQ3TO :: Ord a => Seq3TypeOccurrences a -> Seq3TypeOccurrences a -> Seq3TypeOccurrences a
    unionSQ3TO = M.unionWith (\(n,mp2a) (m,mp2b) -> (n+m,unionSQ2TO mp2a mp2b))
    
    导入符合条件的数据。映射为M
    导入数据列表(排序)
    piDigits=“31415926589793238462643383279502884197169399375105820974944592307816406286208928034825342117067982814808651328230664709384460955082231725359408128481117450281027019385211055596446229954930381964428875666593344844756”
    键入TypesOccurrences a=M。映射一个Int
    ToTypeOccurrencess::Ord k=>[k]->TypesOccurrences k->TypesOccurrences k
    ToTypeOccurrencess[]mp=mp
    totypeexecutions(x:xs)mp=totypeexecutions xs$M.insertWith(+)x 1 mp
    --例如,totypepidigits M.empty
    pprintTO::Show a=>TypesOccurrences a->IO()
    pprintTO=mapM_uuputstrn。映射(\(xs,n)->显示xs++++++(显示n))。排序(\x y->比较(snd y)(snd x))。托利斯特先生
    --例如:普林托。M.过滤器(>22)。totypepidigits$M.empty
    类型Seq2TypeOccurrencess a=M。映射a(Int,类型Occurrences a)
    toSQ2TO::Ord a=>[a]->Seq2TypeOccessments a->Seq2TypeOccessments a
    toSQ2TO[]mp=mp
    toSQ2TO[x]mp=mp
    toSQ2TO(x:y:xs)mp=toSQ2TO(y:xs)$
    案例M.x mp
    无->M.insert x(1,M.singleton y 1)mp
    Just(u,mp2)->的情况M.y mp2
    无->M.update(\(n,mp2)->仅(n+1,M.insert y 1 mp2))x mp
    Just(n,mp2)->Just(n+1,M.update(\M->Just(M+1))y mp2)x mp
    --例如toSQ2TO piDigits M.empty
    pprintSQ2TO::Show a=>seq2typea->IO()
    pprintSQ2TO=mapM_uuputstrln。map(\(x,(n,mp))->“(“++(show x)++”,“++(show n)++”)\n\t“+”(drop 2.concatMap(“\n\t”+).show).M.toList$mp))。托利斯特先生
    --例如pprintSQ2TO(toSQ2TO piDigits M.empty)
    greaterThanSQ2TO::Ord a=>Int->Seq2TypeOccents a->Seq2TypeOccents a
    greaterthansq2ton=M.filter(\(\,mp2)->not.M.null$mp2)。M.map(\(o,mp2)->(o,M.filter(>n)mp2))。M.filter(\(M,mp)->M>n)
    --例如,pprintSQ2TO。大于2到4。toSQ2TO piDigits$M.empty
    descSortSQ2TO::Ord a=>seq2typea->[([a],Int)]
    descSortSQ2TO=sortBy(\xs-ys->compare(snd-ys)(snd-xs))。concatMap(\(x,ys)->zipWith(\x(y,n)->([x,y],n))(重复x)ys)。map(\(x,(\u,mp2))->(x,M.toList mp2))。托利斯特先生
    --地图打印。descSortSQ2TO。大于2到4。toSQ2TO piDigits$M.empty
    unionSQ2TO::Ord a=>Seq2TypeOccessments a->Seq2TypeOccessments a->Seq2TypeOccessments a->Seq2TypeOccessments a
    unionSQ2TO=M.unionWith(\(n1,mp1)(n2,mp2)->(n1+n2,M.unionWith(+)mp1 mp2))
    类型Seq3TypeOccessments a=M。映射a(Int,Seq2TypeOccessments a)
    toSQ3TO::Ord k=>[k]->Seq3TypeInstances k->Seq3TypeInstances k
    toSQ3TO[]mp=mp
    toSQ3TO[x]mp=mp
    toSQ3TO[x,y]mp=mp
    toSQ3TO(x:y:z:xs)mp=toSQ3TO(y:z:xs)$
    案例M.x mp
    Nothing->M.insert x(1,M.singleton y(1,M.singleton z1))mp
    Just(u,mp2)->的情况M.y mp2
    Nothing->M.update(\(n,mp2)->只是(n+1,M.insert y(1,M.singleton z1)mp2)x mp
    Just(m,kns3)->的情况m.z kns3
    Nothing->M.update(\(n,\)->Just(n+1,M.update(\(M,mp3)->Just(M+1,M.insert z 1 mp3))y mp2)x mp
    Just->M.update(\(n,\)->Just(n+1,M.update(\(M,mp3)->Just(M+1,M.update(Just(+1))zmp3))ymp2))x mp
    --例如:toSQ3TO piDigits M.empty
    pprint3::Show a=>seq3typea->IO()
    pprint3=mapM_uuStrln。map(\(x,(n,mp))->“(“++(show x)++”,“++(show n)++”)”+(concatMap(\(x2,(n2,mp2))->“\n\t(“++(show x2)++”,“++(show n2)++”)”+(f mp2)).M.toList$mp))。托利斯特先生
    哪里
    f=concatMap(\(x,n)->“\n\t\t(“++(显示x)++”,“++(显示n)++”)。托利斯特先生
    --pprint3。toSQ3TO piDigits$M.empty
    pprint3B::Show a=>seq3typea->IO()
    pprint3B=mapM_uuuStrln。映射(\(xs,n)->显示xs++++++(显示n))。concatMap(\(xs,mp)->zipWith(\ys(z,n)->(ys++[z],n))(重复xs)mp)。concatMap(\(x,mp)->zipWith(\y(z,mp2)
    
    data Trie = Nil | Trie (Map Char (Int, Trie))
    
    import qualified Data.Map as M
    import Text.PrettyPrint
    import Data.List
    
    data Trie = Nil | Trie (M.Map Char (Int, Trie))
    
    showTrie :: String -> Trie -> Doc
    showTrie _ Nil = empty
    showTrie prefix (Trie m) =
      vcat $ 
        do (k,(count,t)) <- M.assocs m
           let prefix' = prefix ++ [k]
           return $
             vcat [ lparen <> char '"' <> text prefix' <> char '"' <> comma <> int count <> rparen
                  , nest 4 (showTrie prefix' t)
                  ]
    
    -- add an element to a Trie
    addTrie :: Trie -> String -> Trie
    addTrie t [] = t
    addTrie Nil xs = addTrie (Trie M.empty) xs
    addTrie (Trie m) (x:xs) = 
      case M.lookup x m of
        Nothing     -> let t' = addTrie Nil xs
                       in Trie $ M.insert x (1,t') m
        Just (c,t)  -> let t' = addTrie t xs
                       in Trie $ M.insert x (c+1,t') m
    
    test1 = 
      let t1 = addTrie Nil "abcd"
          t2 = addTrie t1  "abce"
      in putStrLn $ render $ showTrie "" t2
    
    test2 n str =
      putStrLn $ render $ showTrie "" $
          foldr (flip addTrie) Nil (map (take n) (tails str))
    
    test3 = test2 4 "31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756"