[Haskell-cafe] Strange behavior with listArray

Alex Stangl alex at stangl.us
Mon Nov 12 14:43:32 CET 2012


On Mon, Nov 12, 2012 at 08:36:49AM +0100, Bas van Dijk wrote:
> On 12 November 2012 04:50, Alex Stangl <alex at stangl.us> wrote:
> > I'm stymied trying to figure out why the program below blows up with
> > <<<loop>>> when I use "f 0"
> If you replace the a!0 in f by its value 0, f is equivalent to:
> 
>             f k = if k > 0
>                     then f 0
>                     else 0 : f 1
> 
> Do you see the loop now?

I realize it loops/recurses, just like h does, or any instance
of building lazy infinite data structures. It works fine when you
only extract a finite number of elements from the infinite structure.
It's not clear why that is not happening here, as if there is a failure
of laziness.  f 0 should effectively yield [0, 0, ...], correct?


> Maybe you meant f to be:
> 
>             f k = if k > 0
>                     then f (a!k)
>                     else 0 : f 1

Actually it was that way in the original program. I switched it to 0
the process of trying to "distill" it down to a simplest test. Either
way yield the same result, <<<loop>>>. If you take the array reference
out, this code works fine, as it obviously should. But with the array
reference intact, it appears listArray isn't accessing the list lazily
enough.

Thanks,

Alex



More information about the Haskell-Cafe mailing list