在Erlang中实现图灵机 [英] Implementing a Turing machine in Erlang

查看:161
本文介绍了在Erlang中实现图灵机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小项目,它非常类似于实现图灵机。我的基本问题是保存当前配置,例如。头的位置和进一步的信息。对我重要的是,特别是节省了头部位置向前或向后移动他。什么是Erlang的方式来解决这个问题?

I have a little project which is very similar to implementing a Turing machine. The essential problem I have is to save the current configuration e.g. the position of the head and further information. Important to me is especially saving up the head position to move him forwards or backwards. What would be the Erlang way to solve this problem?

我是Erlang的新人,但就我所探索的OTP gen_event行为是适合的。我的想法是通过最初的头部位置,然后通过处理程序更改它。

I'm new to Erlang but as far as I explored OTP the gen_event behavior is suited for this. My thought was to pass over the initial head position and then change it via a handler. But I guess there are more elegant solutions.

推荐答案

在Erlang中,和其他函数式语言一样,你必须明确地管理你的状态。这意味着你必须携带它,并通过你的代码。

In Erlang, as in other functional languages, you must explicitly manage your state yourself. This means that you have to carry it with you and thread it through your code. It IS much easier than it sounds and quickly becomes second nature.

我会亲自使用 gen_server c $ c>行为而不是 gen_event 。它更具体,将为您提供更好的支持您的机器。 gen_event 比你需要的更通用。 IMAO。

I would personally use a gen_server behaviour rather than a gen_event. It is more specific and will provide you with better support for your machine. The gen_event is more general than you need. IMAO.

gen_server 行为,事实上,所有的行为都支持管理状态。这些行为提供了顶层循环,接收和发送消息以及管理状态的基本功能。

The gen_server behaviour, all the behaviours in fact, provide support for managing state. The behaviours provide the basic functionality of the top-level loop, receiving and sending message, and managing state. Plus a lot of extra goodies you will want, even if you don't know it yet.

您接口 gen_server

You interface the gen_server, all the behaviours, by providing call back functions which the behaviour calls when something happens. You give the name of a module and the behaviour expects that module to contain the callbacks. Usually there are a fixed number of callbacks, for example the gen_server has 6, with predefined names that are called at specific times.

例如,有一个 init / 1 回调,它在服务器启动时被调用。它执行所有特定的初始化,然后返回 {ok,State} 。这是您的服务器所需的状态。

For example there is an init/1 callback which is called when the server is started. It does all the specific initialisation and then returns {ok,State}. This is the state you need for your server. The behaviour manages this and threads it through the callbacks and expects a new one in return.

例如,当你做一个 gen_server:call(Server ,Message)这将导致在服务器中使用以下参数和返回值调用 handle_call / 3 回调:

For example when you do a gen_server:call(Server, Message) this will result that in the server a call is made to the handle_call/3 callback with the following arguments and return values:

handle_call(Message, From, State)  --> {reply,Reply,NewState}

回覆发送回调用者, NewState 是更新的状态,然后传递到下一个回调。

Reply is sent back to the caller and NewState is the updated state which is then passed into the next callback.

可以在 OTP设计原则中了解详情,例如 Gen_Server行为 gen_server module 部分的文档。

You can read more about this in the OTP Design Principles and for example the Gen_Server Behaviour and the gen_server module sections of the documentation.

在这种情况下,你会让行为是图灵机管理磁带,位置,等等,你会发送命令给它。 IMAO agin。

In you case you would let the behaviour be the Turing machine manage the tape, position, etc and you would send commands to it. IMAO agin.

这篇关于在Erlang中实现图灵机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆