Is there any way to convenient way to express graphs using the tying-the-knot strategy?
Asked Answered
H

1

8

As explained on my previous question, it is impossible to differ two graphs made using the tying the knot strategy if you don't have some kind of unique label on your nodes. Using a two-edged graph as an example:

data Node = Node Int Node Node

square = a where
    a = Node 0 b c
    b = Node 1 a d
    c = Node 2 a d
    d = Node 3 b c

Writing square that way is a little inconvenient and error-prone due to the need of manually writing labels. That kind of pattern would usually call for a monad:

square = do
    a <- Node b c
    b <- Node a d
    c <- Node a d
    d <- Node b c
    return a

But this also can't be done since monads are sequential. Is there any convenient way to write tying-the-knot graphs?

Hebe answered 16/10, 2015 at 0:9 Comment(1)
GIven my other answer, maybe for your particular use case, just tying the knot might be the convenient way!Write
C
10
{-# LANGUAGE RecursiveDo #-}

import Control.Monad.State

type Intividual a = State Int a

data Node = Node Int Node Node

newNode :: Node -> Node -> Intividual Node
newNode a b = state $ \i -> (Node i a b, succ i)

square :: Node
square = (`evalState`0) $ mdo
   a <- newNode b c
   b <- newNode a d
   c <- newNode a d
   d <- newNode b c
   return a
Congressman answered 16/10, 2015 at 0:37 Comment(1)
RecursiveDo, okay, I think I've heard about that someday. Thanks for your answer... can I edit a brief explanation on it?Hebe

© 2022 - 2024 — McMap. All rights reserved.