سؤال

I'm trying to modify the node label of an existing node within a graph that has been constructed using the fgl Haskell package, but I'm unable to understand how to do that. I tried using insNode to reinsert a new node with the same internal node ID and the new label, but I only get a Node Exception when doing that.

MWE:

import Data.Graph.Inductive
import Data.Graph.Inductive.Example

main =
  do let test = insNode (1, 'b') a
     putStrLn $ show a
     putStrLn $ show test
هل كانت مفيدة؟

المحلول 2

I figured out how to do it: you just need to create a new graph with mkGraph using the same list of nodes (but with the appropriate one modified, of course) and edges.

نصائح أخرى

This one is more elegant and efficient:

updateLabel g n updateFun = case match n g of
      (Just (p, _, l, s), cg) -> (p, n, updateFun l, s) & cg
      (Nothing,            _) -> error "not existent node!?"

Or if you are not sure if g - the graph - contains n - the node, go with:

updateLabel g n updateFun = case match n g of
      (Just (p, _, l, s), cg) -> Just $ (p, n, updateFun l, s) & cg
      (Nothing,            _) -> Nothing
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top