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.

Today I lost a lot of time with a strange behavior and I narrowed it down to this small piece of code:

a = {1, 2};
ToString@Row[DeleteDuplicates[a], ","]
ToString@Row[Union[a], ","]
"Row[{1, 2}, ,]"
"1,2"

Can someone explain this bizarre behavior? Is this a bug?

This is interesting too:

a = {1, 2};
l1 = DeleteDuplicates[a];
l2 = Union[a];
l1==l2
s1 = ToString@Row[l1, ","]
s2 = ToString@Row[l2, ","]
s1==s2
True
"Row[{1, 2}, ,]"
"1,2"
False

There is some "memory" in l1 created using DeleteDuplicates.

UPDATE

This bug was fixed in V10.0.0

share|improve this question
    
Interesting. If you Apply List to the DeleteDuplicates, results are peachy. –  rasher Feb 3 at 21:00
1  
@Murta: Might want to change title to Row instead of DeleteDuplicates... a bit less alarming ;-) –  rasher Feb 3 at 21:14
1  
please don't use the bugs tag when asking a question... if users (or WRI support/devs) confirm in the comments that it is indeed a weird behaviour/bug, then it can be retagged as such. –  rm -rf Feb 3 at 21:24
2  
@rm-rf It was confirmed by Wolfram Support. We can tag it. –  Murta Feb 3 at 21:43
1  
Appears to have been fixed in V10 –  Mike Honeychurch Jul 16 at 4:09

2 Answers 2

up vote 11 down vote accepted

Actually this happens when Row contains a packed array. DeleteDuplicates packs the array. Union doesn't. A simpler way to replicate the behaviour:

ToString@Row[Developer`ToPackedArray[{1,2}], ","]

(* ==> "Row[{1, 2}, ,]" *)

The default form used by ToString is OutputForm and the following also exhibits the problem:

OutputForm[Row[Developer`ToPackedArray[{1, 2}], ","]]

(* ==> Row[{1, 2}, ,] *)

I can reproduce it in both 8.0.4 and 9.0.1. I'd say it's a bug and you should report it to support.

share|improve this answer
1  
Tks @Szabolcs, just reported (CASE:628688). Reproduced in V10 beta too. Let's see if final version 10 solves that. –  Murta Feb 3 at 21:14
    
Quick-draw analysis! +1 –  rasher Feb 3 at 22:14

As Szabolcs notes this occurs only when the first argument of Row is a packed array. In this case DeleteDuplicates returns a packed array while Union does not, which explains that difference:

<< Developer`
a = {1, 2};
b = Union @ a;
c = DeleteDuplicates @ a;
PackedArrayQ /@ {b, c}
{False, True}

One can see that this is not an issue of Row itself as these render as expected:

Row[b, ","]
Row[c, ","]
1, 2
1, 2

Rather, The variance occurs in OutputForm, which is normally not used in the Notebook interface, but which is the format that ToString defaults to:

Row[b, ","] // OutputForm
Row[c, ","] // OutputForm
1,2

Row[{1, 2}, ,]

I suspect that since Row was introduced in version 6, and it is ostensibly a 2D formatting function (it is not merely a string concatenation utility), someone forgot to update OutputForm to work in this edge case. For the case at hand one can easily avoid the problem by specifying the default Notebook format, StandardForm:

ToString[Row[b, ","], StandardForm]
ToString[Row[c, ","], StandardForm]
"1, 2"
"1, 2"
share|improve this answer
    
Much better use StandardForm instead of FromPackedArray as I was doing. Tks –  Murta Feb 4 at 10:41
    
Change my mind.. Now using StringJoin@Riffle[ToString /@ a, ","] –  Murta Feb 4 at 10:53

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.