Haskell Data.Map的内部表示

时间:2019-02-08 05:12:02

标签: dictionary haskell data-structures

如何查看Haskell Data.Map的内部表示形式? 另外,使用哪种数据结构来实现它? 它本质上是一棵红黑树吗? 还是某种堆分钟? 这是一个我有兴趣转储到文本文件的简单示例 (甚至更好的是Graphviz dot表示形式)。

module Main( main ) where

import qualified Data.Map as Map

t = Map.fromList([
    (6,"six"),
    (2,"two"),
    (8,"eight"),
    (3,"three"),
    (5,"five")])

main = do
    putStrLn $ show $ Map.lookup 3 t
    putStrLn $ show $ Map.lookup 7 t

2 个答案:

答案 0 :(得分:3)

以下是带有showTreeWith的文本转储:

module Main( main ) where

import qualified Data.Map as Map

t = Map.fromList([
    (6,"six"),
    (2,"two"),
    (8,"eight"),
    (3,"three"),
    (5,"five")])

main = do
    putStrLn $ Map.showTreeWith (\k x -> show (k,x)) True False t
    putStrLn $ show $ Map.lookup 3 t
    putStrLn $ show $ Map.lookup 7 t

这是它的外观:

$ ghc main.hs
$ ./main
(6,"six")
+--(3,"three")
|  +--(2,"two")
|  +--(5,"five")
+--(8,"eight")

Just "three"
Nothing

答案 1 :(得分:0)

根据the documentation for Haskell's Data.Map

  

Map的实现基于大小平衡的二叉树(或有限平衡树)