【问题标题】:How do I modify a record in erlang?如何修改erlang中的记录?
【发布时间】:2010-04-26 17:32:18
【问题描述】:

我需要修改操作记录中的值 {place} 和 {other_place}。

#op{
    action   = [walk, from, {place}, to, {other_place}],
    preconds = [[at, {place}, me], [on, floor, me], 
                [other_place, {place}, {other_place}]],
    add_list = [[at, {other_place}, me]],
    del_list = [[at, {place}, me]]
}

但是 erlang 不允许修改变量。有相应的数据类型吗?

【问题讨论】:

  • 请用 4 个缩进空格格式化您的代码。 (或选择它们并按 ctrl+K。)
  • 在 Erlang 中解决 NP 完全问题的方法与解决其他问题的方法相同,只是需要更长的时间。请至少重新命名您的问题。

标签: erlang record immutability


【解决方案1】:

erlang 不允许你修改变量,这是真的。但是没有什么能阻止你修改变量的副本。

鉴于你的记录:

Rec = #op{
    action   = [walk, from, {place}, to, {other_place}],
    preconds = [[at, {place}, me], [on, floor, me], 
                [other_place, {place}, {other_place}]],
    add_list = [[at, {other_place}, me]],
    del_list = [[at, {place}, me]]
}

您可以像这样有效地获得修改后的版本:

%% replaces the action field in Rec2 but everything else is the same as Rec.
Rec2 = Rec#op{action = [walk, from, {new_place}, to, {new_other_place}]}

这将完成您的要求。

【讨论】:

  • 呃,让我搞砸了我的脸。我放弃了 Erlang
猜你喜欢
  • 2015-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-04
  • 1970-01-01
  • 2011-11-04
  • 1970-01-01
相关资源
最近更新 更多