Short answer: it will only be created before the item you are looking for. This means that only in the worst case you will need to build the entire list, that is, when not a single element satisfies the conditions.
Long answer: let me explain why with a couple of examples:
ghci> head [a | (a,b) <- zip [1..] [1..], a > 10] 11
In this case, zip should create an infinite list, but the tape allows Haskell to build it only up to (11,11) : as you can see, the execution does not diverge, but in fact gives us the correct answer.
Now let me consider another problem:
ghci> find_first_occurrence 1 [0, 0, 1 `div` 0, 1] *** Exception: divide by zero ghci> find_first_occurrence 1 [0, 1, 1 `div` 0, 0] 1 it :: Int (0.02 secs, 1577136 bytes)
Since the entire zip list is not built, haskell, obviously, will not even evaluate every expression that occurs in the list, therefore, when the element is before div 1 0 , the function is correctly calculated without any exceptions: division by zero does not occur.
source share