Bug introduced in 8.0.0 and fixed in 8.0.1

I have to following code that solves a problem for me. The example that is given to us is as follows: lst = {1,-2, 3, 4,5,-3, -4, 9,7,0,8}, predicate = Positive. The maximal Predicate-segments of lst are {1}, {3,4,5}, {9,7}, and {8}.

getSegments[lst_,predicate_] :=
  Module[{res= {},seg, lstcopy=lst},
      seg = TakeWhile[lstcopy,predicate[#]&];
      If[Length[seg] > 0,
        (* True *)
        (* False *)
      ] (* If *)
    ] ;(* While *)

The above code works fine for the following input:

In[1]:= getSegments[RandomInteger[{-100,100},10],Positive]
Out[1]= {{68,13},{59},{16},{41}}

In[2]:= getSegments[RandomInteger[{-100,100},10],Negative]
Out[2]= {{-26},{-77,-60},{-11,-78}}

Yet something very strange happens with the following:

In[3]:= getSegments[RandomInteger[{1,2},10],EvenQ]
Out[3]= {}

In[4]:= getSegments[{1,1,2,2,1,2,2,1,2,2},EvenQ]
Out[4]= {{2,2},{2,2},{2,2}}

This is the same for OddQ.

Why does it treat the RandomInteger result so differently from the list?

The input for In[4] is also generated with a call to RandomInteger[{1,2},10]. I did find that seg is always empty with the In[3] case, while the False branch of the If takes all the elements in the list.

EDIT It is of note that I run Mathematica 8 on Linux. The above works fine on Windows versions of Mathematica.


In[76]:= getSegments[RandomInteger[{1,2},5],EvenQ] // Trace
Out[76]= {{RandomInteger[{1,2},5],

I've trimmed down to problem to the following:

lst = RandomInteger[{1, 2}, 10]
TakeWhile[lst, EvenQ]
TakeWhile[lst, OddQ]

One of the TakeWhile calls should always return some value. Yet they both give an empty list. This works fine on Windows, yet fails on Linux.

  • 1
    $\begingroup$ getSegments[RandomInteger[{1,2},10], EvenQ] works as expected on my system (Mma v8.0.4 Windows Vista 64bit). $\endgroup$
    – kglr
    Dec 20, 2012 at 10:58
  • $\begingroup$ @kguler Yes, I've tried that too and see that it works on Windows. It seems to be a Linux specific problem. $\endgroup$
    – Chrono
    Dec 20, 2012 at 11:02
  • 1
    $\begingroup$ I found an older question that relates to this. It's apparently a known bug in version 8.0.0 so I'm adding those tags. $\endgroup$
    – Mr.Wizard
    Dec 20, 2012 at 11:42

1 Answer 1


I don't know why it doesn't work on your system (it does mine), but perhaps this will help:

getSegments[lst_, test_] := 
  SplitBy[lst, test][[ If[test @ First @ lst, 1, 2] ;; ;; 2 ]]

getSegments[{1, -2, 3, 4, 5, -3, -4, 9, 7, 0, 8}, Positive]
{{1}, {3, 4, 5}, {9, 7}, {8}}

I found an older question that relates to this. It's apparently a known bug in version 8.0.0.
You will need to unpack your data before passing it to TakeWhile.

lst = RandomInteger[{1, 2}, 10];

TakeWhile[Developer`FromPackedArray @ lst, EvenQ]
  • $\begingroup$ While that does solve the problem (not to mention in a much nicer way :)), it does not give me an answer to my problem. $\endgroup$
    – Chrono
    Dec 20, 2012 at 11:19
  • 1
    $\begingroup$ @Willem I wish I had an answer for you other than "could not reproduce" but I'm using Windows. Would you append the output of getSegments[RandomInteger[{1, 2}, 10], EvenQ] // Trace on your system to the question? $\endgroup$
    – Mr.Wizard
    Dec 20, 2012 at 11:24
  • $\begingroup$ I've added the Trace result. I also trimmed down the problem to just the TakeWhile. $\endgroup$
    – Chrono
    Dec 20, 2012 at 11:34
  • $\begingroup$ @Willem sorry, I wasn't clear; I meant your getSegments, but I see that you've isolated the problem to TakeWhile. I now suspect packing. Please try: lst = Developer`FromPackedArray@RandomInteger[{1, 2}, 10]; then TakeWhile[lst, EvenQ] and TakeWhile[lst, OddQ] and report if then works correctly. $\endgroup$
    – Mr.Wizard
    Dec 20, 2012 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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