![]() |
  |
|
5.5.2.3 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 5.2.7.3.2. 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).
|
|||||||||||||||||