문제

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