Plan 9 from Bell Labs’s /usr/web/sources/contrib/fernan/nhc98/src/hmake/Tsort.hs

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


-----------------------------------------------------------------------------
-- |
-- Module      :  Tsort
-- Copyright   :  Thomas Hallgren?  Lennart Augustsson?
-- 
-- Maintainer  :  Malcolm Wallace <[email protected]>
-- Stability   :  Stable
-- Portability :  All
--
-- partial topological sort
-----------------------------------------------------------------------------

module Tsort(ptsort) where
import Compat(difference)
import List

-- | partial topological sort
--
--		ptsort G sorts the graph G.  A graph is a list of nodes, each
--		node is a pair, a name and a list of names of connected nodes.
--
--		The output is a list of lists, where the lists contain nodes
--		that come before nodes occuring in later lists.
--		The order of nodes within each list is arbitrary and not
--		determined by the graph.
ptsort :: (Eq a) => [(a, [a])] -> [[a]]
ptsort [] = []
ptsort gG =
    case partition (\(_, x) -> null x) gG of
      ([], _) -> error "ptsort: cycle in data\n"
      (a, b) -> let a' = map fst a
                in  a' : ptsort (map (\(x, xs) -> (x, difference xs a')) b)

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].