Python 和 F# 中的递归变量定义(可能还包括 OCaml)

5 投票
2 回答
1286 浏览
提问于 2025-04-16 12:32

给定这些F#的类型声明...

type Message =
    | MessageA
    | MessageB
    | MessageC
    | MessageD

type State = {
    Name:string
    NextStateMap: Map<Message,State>
}

...有没有一种同样表达力强的方式来定义这个特定的状态机...

let rec state0 = { Name = "0"; NextStateMap = Map.ofList [ (MessageA,state1); (MessageB,state2)] }
    and state1 = { Name = "1"; NextStateMap = Map.ofList [ (MessageB,state3)] }
    and state2 = { Name = "2"; NextStateMap = Map.ofList [ (MessageA,state3)] }
    and state3 = { Name = "3"; NextStateMap = Map.ofList [ (MessageC,state4)] }
    and state4 = { Name = "4"; NextStateMap = Map.ofList [ (MessageD,state5)] }
    and state5 = { Name = "5"; NextStateMap = Map.empty}

...用Python来实现呢?

注意,通过“rec”,我们不需要按照某种特定的顺序来进行赋值,比如说,state0是基于state1来定义的,尽管state1是在后面定义的。

附注:使用字符串作为状态标识符的选项...

stateMachine = {
   "0" : { "A":"1", "B":"2"},
   "1" : { "B":"3" },
...

...会留下无效键的情况(也就是说,在状态机中无效的消息标识符)。

2 个回答

0

在编程中,有时候我们会遇到一些问题,比如代码运行不正常或者出现错误。这时候,我们可以去一些技术论坛,比如StackOverflow,寻求帮助。在这些论坛上,很多人会分享他们的经验和解决方案,帮助其他人解决类似的问题。

当你在这些论坛上提问时,记得把你的问题描述清楚,包括你遇到的错误信息和你尝试过的解决方法。这样,其他人才能更好地理解你的问题,并给出有效的建议。

有时候,别人可能会给你一些代码示例,帮助你更好地理解解决方案。你可以根据这些示例来修改自己的代码,看看能否解决问题。

总之,利用这些技术论坛是一个很好的学习和解决问题的方式,只要你能清楚地表达自己的问题,就能得到很多有用的帮助。

## a generic state machine framework ###################

class Message(object):
    """
    This represents a message being passed to the
    state machine.
    """
    def __init__(self, name):
        self.name = name
    def __str__(self):
        return "Message(%r)" % self.name
    def __call__(self, smap):
        try:
            return smap[self]
        except KeyError:
            raise Exception("invalid message: %s vs %s"
                            % (self, smap))

class MessageFactory(object):
    """
    Since python doesn't have symbols, this automagically
    creates the messages for you. (It's purely for
    convenience, and you could just as easily instantiate
    each message by hand.
    """
    cache = {}
    def __getattr__(self, name):
        return self.cache.setdefault(name, Message(name))

class StateMachine(object):
    """
    This keeps track of the state, of course. :)
    """
    def __init__(self, state):
        self.state = state
    def __call__(self, msg):
        self.state = self.state(msg)


## how to set it up: ###################################

msg = MessageFactory()
state =\
{
    0 : lambda m: m({ msg.A : state[1],
                      msg.B : state[2] }),
    1 : lambda m: m({ msg.B : state[3] }),
    2 : lambda m: m({ msg.A : state[3] }),
    3 : lambda m: m({ msg.C : state[4] }),
    4 : lambda m: m({ msg.D : state[5] }),
    5 : lambda m: m({ }),
}

## how to use it: ######################################

s = StateMachine(state[0])
s(msg.A)
assert s.state is state[1]
5

在Python中,我觉得你需要先定义状态,然后再设置地图。可以用类似下面的伪代码来表示:

state0 = State("0")
state1 = State("1")
... and so on ...
state0.next_states = {message_a: state1, message_b: state2 }
state1.next_states = {message_b: state3}
... and so on ...

撰写回答