Say you have a very deterministic algorithm that produces a list, like inits
in Data.List
. Is there any way that a Haskell compiler can optimally perform an "indexing" operation on this algorithm without actually generating all the intermediate results?
For example, inits [1..] !! 10000
is pretty slow. Could a compiler somehow deduce what inits
would produce on the 10000th element without any recursion, etc? Of course, this same idea could be generalized beyond lists.
Edit: While inits [1..] !! 10000
is constant, I am wondering about any "index-like" operation on some algorithm. For example, could \i -> inits [1..] !! i
be optimized such that no [or minimal] recursion is performed to reach the result for any i
?