Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

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]
share|improve this answer
    
An almost identical answer so I deleted mine. Just 1 minute difference... –  Sjoerd C. de Vries May 20 '12 at 17:36
    
It works. But then I tried using board_Graph instead of board_ /; Head[board] === Graph and it didn't work. Why? –  becko May 20 '12 at 18:01
1  
@becko Because with HoldFirst you're now not sending a Graph but a symbol referring to a Graph, which isn't the same. –  Sjoerd C. de Vries May 20 '12 at 18:24
    
@SjoerdC.deVries Got it. –  becko May 20 '12 at 18:37
1  
@becko But Set does have the attribute HoldFirst, so in PropertyValue[....] = new, the whole left-hand side is held, including the Graph object. –  Heike May 22 '12 at 8:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.