[Haskell-beginners] Stack space overflow: using strict accumulator still fails

Hugo Ferreira hmf at inescporto.pt
Mon Oct 31 23:42:30 CET 2011


On 10/31/2011 10:31 PM, Daniel Fischer wrote:
> On Monday 31 October 2011, 23:12:03, Hugo Ferreira wrote:
>> You mean, I have to use something like:
>>
>>     where
>>       (correct,_) = Z.cursor z
>>
>> instead of
>>
>>     where (correct,_) = Z.cursor z
>
> Yes.
>
>>
>> I googled the rules and found
>> http://en.wikibooks.org/wiki/Haskell/Indentation. Doesn't seem to be a
>> requirement.
>
> Hence the ";)".
>

I see. B-)

>
>>>
>>> Been a bugger to hunt down, but I finally found it.
>>> The culprit is *drumroll, ba-dum tish*:
>>> a one-character typo in Data.List.Zipper.
>>> foldlz' isn't. The recursive call is to foldlz, not foldlz'.
>>
>> Now I am baffled B-(
>>
>> The package docs state:
>>     foldlz' is foldlz with a strict accumulator
>>
>> Why would/should one not use the strict version here?
>
> Because there's a typo in the implementation (notified the maintainer).
>
>> What do you mean when you say "recursive call"?
>
> Let's look at lists first.
>
> The specification in the standard is
>
> foldl f z [] = z
> foldl f z (x:xs) = foldl f (f z x) xs
>                     ^^^^^
>             recursive call to foldl
>
> Very similar for the list zipper, we have
>
> foldlz :: (b ->  Zipper a ->  b) ->  b ->  Zipper a ->  b
> foldlz f x z
>          | endp z    = x
>          | otherwise = foldlz f (f x z) (right z)
>                        ^^^^^^
>
> And for foldlz', the intention was
>
> foldlz' :: (b ->  Zipper a ->  b) ->  b ->  Zipper a ->  b
> foldlz' f x z
>          | endp z    = x
>          | otherwise = acc `seq` foldlz' f acc (right z)
>          where acc = f x z
>
> but in the otherwise branch, the ' has been omitted, so instead of
> recursively calling itself with the new accumulator, it calls foldlz.
>

Ok, I though the typo was on my side.
So you have found a bug.
My hat is off to you.

Thanks,
Hugo F.





More information about the Beginners mailing list