Re: part assigned sequence behavior puzzling
- To: mathgroup at smc.vnet.net
- Subject: [mg105393] Re: part assigned sequence behavior puzzling
- From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
- Date: Tue, 1 Dec 2009 04:13:03 -0500 (EST)
- References: <hekujm$96g$1@smc.vnet.net> <hf09b2$d1s$1@smc.vnet.net>
Hi Daniel, Actually, in all my years with Mathematica I haven't had a single occasion in which Sequence would used in this way, so perhaps the whole discussion is moot. > (1) What is the expected/intended behavior of > > In[87]:= ss = {a, b, c, d}; Do[ss[[j]] = Sequence[j, j + 1], {j, > Length[ss]}] Well, I know now that it is supposed to be {1, 2, 2, 3, 3, 4, 4, 5}, but my intuition would say {1, 2, 3, 4, 5, b, c, d}. Look at this sequence (sic) of instructions (and execute it as a whole): s = {1, 2, 3} s[[1]] = Sequence[4, 5] s[[1]] s[[2]] s[[2]] = DifferentValueForIndex2 s[[2]] s The first time you ask for the contents of s[[2]] it is 5 (and this is the only 5 you put in the list), then you assign something else to s [[2]], so any average programmer would think the 5 is gone out of the list now. But, surprise, there it still is... big magic I'd say if you can check and use the value at index 2 for calculations, you should be able to set the value at index 2, but you can't in this case. I wonder how many of your colleagues, when shown the subsequence of s[[2]] s[[2]] = DifferentValueForIndex2 s[[2]] without showing them the first part, will be utterly amazed, and won't know what's causing this. Cheers -- Sjoerd On Nov 30, 1:12 pm, d... at wolfram.com wrote: > > I'd say that this is not a wise design decision. IMHO, a statement > > like tmp=tmp should have no effect on tmp, whereas with this > > SequenceHold situation it has. > > > Cheers -- Sjoerd > > Perhaps so. I view it, paraphrasing Churchill, as the worst possible > design, except for all the rest. > > In the alternative scenario, Sequence[...] things would (I guess) cause > immediate evaluation of the left hand side. This has some bad consequence= s > and maybe also raises a few questions. > > Bad consequences: > > (1) It changes assignment semantics. > (2) It means that deeply nested expressions cannot readily be built > because complexity of adding a level becomes worse (maybe this can largel= y > be short-circuited, I'm not sure). > > Questions: > > (1) What is the expected/intended behavior of > > In[87]:= ss = {a, b, c, d}; Do[ss[[j]] = Sequence[j, j + 1], {j, Le= ngth[ss]}] > ? > > (2) What about > ss2 = Table[Unevaluated[Sequence[i, i^2]], {i, 4}] > ? > > Maybe that second one is obvious, and no different from current behavior. > > All the same, I think it would not be good to have a semantics wherein th= e > assignment in the Do loop causes evaluation of the lhs in each iteration. > > Daniel Lichtblau > Wolfram Research > > > On Nov 26, 6:01 am, Bob Hanlon <hanl... at cox.net> wrote: > >> As stated in the documentation for sequence: assignment and replacemen= t > >> f= > > unctions have attribute SequenceHold > > >> Attributes[Set] > > >> {HoldFirst,Protected,SequenceHold} > > >> tmp = Range[15] > >> tmp[[7]] = Sequence @@ Range[2]; > >> tmp[[7]] = Sequence @@ Range[2]; > >> tmp[[7]] = Sequence @@ Range[2]; > >> tmp > > >> {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15} > > >> {1,2,3,4,5,6,1,2,8,9,10,11,12,13,14,15} > > >> tmp = Range[15] > >> tmp[[7]] = Sequence @@ Range[2]; > >> tmp = tmp; > >> tmp[[7]] = Sequence @@ Range[2]; > >> tmp = tmp; > >> tmp[[7]] = Sequence @@ Range[2]; > >> tmp > > >> {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15} > > >> {1,2,3,4,5,6,1,2,2,2,8,9,10,11,12,13,14,15} > > >> Bob Hanlon > > >> ---- mkr <mileskra... at gmail.com> wrote: > > >> ============= > >> I am puzzled by the following behavior: > > >> tmp = Range[15] > >> tmp[[7]] = Sequence @@ Range[2]; > >> tmp[[7]] = Sequence @@ Range[2]; > >> tmp[[7]] = Sequence @@ Range[2]; > >> tmp > > >> yields > > >> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} > >> {1, 2, 3, 4, 5, 6, 1, 2, 8, 9, 10, 11, 12, 13, 14, 15} > > >> I would have expected the repeated assignment to have a repeated > >> effect, thus obtaining > > >> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} > >> {1, 2, 3, 4, 5, 6, 1, 2, 2, 2, 8, 9, 10, 11, 12, 13, 14, 15} > > >> Where/why am I wrong?