I created this function:
AddStone[board_Graph, v_, s : white | black | empty] :=
PropertyValue[{board, v}, VertexState] = s
This function should change the VertexState property of vertex v in graph board to one of white, black, or empty. That's simple enough, right? Then I created a graph:
board = GridGraph[{5, 5}];
But then if I execute:
AddStone[board, 8, white]
I get an error:
"Set::setraw: Cannot assign to raw object PropertyValue[{ ... ,8},VertexState]=white."
where the "..." stands for an image of the graph board, which I obviously need not draw here.
In the Documentation for PropertyValue it is stated that one can set properties this way. In fact, the code
PropertyValue[{board, 4}, VertexState] = white
does work without issuing any messages.
Answer
By default, a function evaluates it's arguments before plugging them into the function definition, so in effect AddStone is trying to set the property of the value of board instead of board itself. To get around this you could set the attribute HoldFirst which tells a function to substitute the unevaluated form of the first argument into the right-hand side. So for example
Clear[AddStone];
SetAttributes[AddStone, HoldFirst];
AddStone[board_ /; Head[board] === Graph, v_, s : (white | black | empty)] :=
(PropertyValue[{board, v}, VertexState] = s)
board = GridGraph[{5, 5}];
AddStone[board, 8, white]
PropertyValue[{board, 8}, VertexState]
Comments
Post a Comment