splitAt is implemented in GHC in this way:
splitAt n xs = (take n xs, drop n xs)
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.
Regarding your second question, recursive process are usually what you want when you're dealing with lists, because they're a spine-lazy data structure. Consider the following the code:
head (take 100 [1 ..])
If we follow the code, we'll eventually arrive to the following expression: 1 : take (100 - 1) [2 ..]
. Since we're only interested in the head of the list, the recursive call to take
is never evaluated. However if take
were to be implemented as an iterative process, it would need to iterate through all 100 elements, even if only the first element is needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With