Mai*_*tor 8 syntax haskell graph tying-the-knot
正如我在上一个问题中所解释的那样,如果您的节点上没有某种独特的标签,则不可能将使用结点策略制作的两个图表区分开来.使用双刃图作为示例:
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
Run Code Online (Sandbox Code Playgroud)
square由于需要手动编写标签,因此以这种方式编写有点不方便并且容易出错.这种模式通常需要monad:
square = do
a <- Node b c
b <- Node a d
c <- Node a d
d <- Node b c
return a
Run Code Online (Sandbox Code Playgroud)
但由于monads是连续的,所以也无法做到这一点.有没有方便的方法来编写结图?
lef*_*out 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
Run Code Online (Sandbox Code Playgroud)