Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I modify a record in erlang?

I to need modify the values {place} and {other_place} in the op record.

#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]]
}

But erlang don´t allow modifying variables. Is there a data type for that?

like image 864
Yadira Suazo Avatar asked Apr 26 '10 17:04

Yadira Suazo


1 Answers

erlang doesn't let you modify variables it is true. But nothing prevents you from making modified copies of a variable.

Given your record:

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]]
}

You can effectively get a modified version like so:

%% 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}]}

This will accomplish what you seem to be asking.

like image 72
Jeremy Wall Avatar answered Oct 17 '22 09:10

Jeremy Wall