Haskell で配列を使いたい場合,Data.Array.* を使うよりも,Data.Vector を使う方が良いらしい(Stack Overflowにあるすばらしい記事).
Tree の parent 関係が与えられたときに,child list を作成する例:
import qualified Data.Vector as V import qualified Data.Vector.Unboxed as VU import qualified Data.Vector.Mutable as VM import Control.Monad.ST import Data.Foldable -- We assume that the root node is 0. foo[i] is the parent node of node (i+1) foo :: VU.Vector Int foo = VU.fromList [0, 0, 1, 2, 0, 3, 1, 4] -- revv[i] is the list of children of node i revv :: V.Vector [Int] revv = V.create f where f :: ST s (V.MVector s [Int]) f = do let len = VU.length foo v <- VM.replicate (len + 1) [] for_ [1 .. len] $ \i -> VM.modify v (i :) (foo VU.! (i - 1)) return v -- We could write "V.create $ do ...", but here I want to write the type of f.
もちろん,revv を作るためだけなら foo を Vector にする必要はない.