GHC splitAt performance
ghc, haskell
Solution
The definition you are looking at is the Haskell Report Prelude definition.
Quoting from that page (emphasis mine)
In this chapter the entire Haskell Prelude is given. It constitutes a specification for the Prelude. Many of the definitions are written with clarity rather than efficiency in mind, and it is not required that the specification be implemented as shown here.
So in the GHC source, when you see
#ifdef USE_REPORT_PRELUDE
// Haskell report prelude definition here (inefficient)
#else
// Efficient definition here
#endif
you should read the `#else` branch if you want to see the definition that will normally be used - unless you specifically ask for the Haskell Report definitions.
Problem
splitAt is implemented in GHC in this way: ``` splitAt n xs = (take n xs, drop n xs) ``` - So does, splitAt do double the work or is there some behind the curtain optimization? - Furthermore, take and drop generate a recursive process. Why is that. After all they are library functions and beauty is not as important. Why aren't they implemented to create an iterative process?