Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I have built a function which carries out a set of operations given two parameters. The function looks as follows:

playingHand[Player_, Name_String] :=
 Row[{Style[Name <> ":", FontSize -> 48, FontFamily -> "Calibri"],
   If[Player == {}, {Style["Not playing", 48, Gray, 
      FontFamily -> "Calibri"]},
       With[{i = i},
        Player[[i, 3]] :>
         {AppendTo[discardPile, Player[[i]]], 
          Player = Delete[Player, i]}],
       {i, Length@Player}]]}]]

The gist of this function is that as its first input it takes a list, which looks something like:

enter image description here

and displays the third elements of each list within that list:

enter image description here

It also makes these images clickable, so that when one of these cards is clicked, it should disappear from player1 and get added to an initially empty list called discardPile. So far so good. When I run this function by itself without the defined function, namely like this:

   With[{i = i},
    player1[[i, 3]] :>
     {AppendTo[discardPile, player1[[i]]], 
      player1 = Delete[player1, i]}],
   {i, Length@player1}]] // Dynamic

it works like a treat. The cards are clickable and they disappear from the ButtonBar as soon as they are clicked and added to discardPile. When I try to do the same from within the function though, namely playingHand[player1, "Aron"] // Dynamic it suddenly stops working. A Set::shape error message comes up warning that two lists are not the same length, and although it does add the card to discardPile, it does not remove it from player1. So:

  1. What is wrong with this function and why won't it delete the card from the list when it is clicked, and
  2. How come the same error does not occur when I run the function directly without calling it by its function name?
share|improve this question

marked as duplicate by Mr.Wizard May 4 '14 at 10:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Please provide a self-contained example. – Mr.Wizard May 3 '14 at 22:52
This question has an answer here – m_goldberg May 4 '14 at 1:25
up vote 3 down vote accepted

You need to set a Hold-ish attribute on playingHand to preserve your first parameter as a symbol when it is replaced into the body of the function.

SetAttributes[playingHand, HoldFirst];
playingHand[Player_, Name_String] := ...

You have to do this because your function body is assigning to Player, and you can't assign to the explicit list that gets inserted for Player when your function is invoked. For example, something basically like this is happening there:

In[11]:= {1, 2, 3} = Delete[{1, 2, 3}, 2]

During evaluation of In[11]:= Set::shape: Lists {1,2,3} and {1,3} are not the same shape. >>

Out[11]= {1, 3}
share|improve this answer
you are a genius. I am eternally grateful to you! – Aron May 3 '14 at 23:41

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