If I define an infinite list and call map over it with a function, I can take some number of elements from the beginning and Haskell will generate and apply the function only to these elements due to lazy evaluation
-- This will generate and process 10 elements
doubledNumbers = map (*2) [0..]
take 10 doubledNumbers
If I index this infinite mapping instead
doubledNumbers !! 9
will Haskell generate the list up to that point, apply the mapping function to all elements and give the element at index 9 back; or will it generate the list, apply the mapping function to the element at index 9, and give that back. In other words, which of the following (if any) would happen when I call doubledNumbers !! 9
- The list [0,1,2,3,4,5,6,7,8,9] is generated, then the list [0,2,4,6,8,10,12,14,16,18], then 18 is taken from that list
- The list [0,1,2,3,4,5,6,7,8,9] is generated, than 9 is taken from that list and given to (*2) , than (*2) returns 18