On Tue, Aug 15, 2017 at 1:33 AM, Ian Kelly <ian.g.ke...@gmail.com> wrote: > On Sun, Aug 13, 2017 at 8:36 AM, Steve D'Aprano >> Sure. In Haskell, comprehensions are *implicit* loops, rather than explicit >> like >> in Python. > > No, they aren't. Haskell list comprehensions use lazy evaluation. > Here's an example of an infinite list comprehension: > > Prelude> let squares = [ x ** 2 | x <- [1 ..] ] :: [Float] > Prelude> :print squares > squares = (_t1::[Float]) > > You might say that this is more like a generator expression but the > result is in fact a list. We can evaluate the first four elements: > > Prelude> print $ take 4 squares > [1.0,4.0,9.0,16.0] > > And then see that these have been lazily evaluated in the list: > > Prelude> :print squares > squares = 1.0 : 4.0 : 9.0 : 16.0 : (_t2::[Float]) > > A Haskell list comprehension is not a loop at all.
What if you don't take the first four, but instead take just the tenth element? Will the preceding elements be calculated too, or do you have a sparse list? If the latter, it's not really comparable to a Python list, but to some sort of cached mapping from input values to output values, which in Python I would implement as a dict with a __missing__ method. And if the former, well, that's still going to have a loop, and it's definitely like a genexp, but genexps have more functionality than they do in Python. ChrisA -- https://mail.python.org/mailman/listinfo/python-list