Read Online
Download PDF
Additional resources
Revision history
Known typos/bugs
Report a bug
License terms
About the author
"node520_1.gif" "node520_2.gif" "node520_3.gif"    Application: splitting the list into sublists of specified lengths (generalized Take operation)

We can put the just developed function into a direct use to create a quite useful one: given a list and the list of partial lengths, split the original list into sublists of these lengths. Here is the code :


Check :



The final comment here is that this is an example of redundant error - checking as discussed at the end of  section  To eliminate it in this case, it is best to embed the body of the < splitIntervals > function inside the < splitList > function, since it is really short (if we would have to write several functions using < splitIntervals >, we would perhaps be better off putting everything into a package and making < splitIntervals > a private (internal) function - then we can remove type checks):


To see how much overhead the redundant type - checks induce in this case, we can perform a power test :





We see that both functions are quite fast (this is  one of the fastest implementations of this function in Mathematica that I know of), and the difference is of the order of 10 %. The difference would be more considerable if the patterns used in error - checking were more semantic (here they are mostly syntactic).

"node520_12.gif" "node520_13.gif" "node520_14.gif"

Created by Wolfram Mathematica 6.0  (05 February 2009) Valid XHTML 1.1!