[Haskell-cafe] Re: Diagnosing stack overflow

apfelmus apfelmus at quantentunnel.de
Fri Aug 17 12:04:23 EDT 2007


Justin Bailey wrote:
> apfelmus wrote:
> 
>> Extracting the head and tail of  ss  with a let statement could lead to
>>   a huge unevaluated expression like
>>
>>    rest = tail (tail (tail (...)))
> 
> Even though they are probably forced, would breaking the head and tail
> apart via pattern-matching or a case statement avoid building up that
> unevaluated expression?

Yes, absolutely, since pattern matching has to force the scrutinee in 
order to choose the matching case. In contrast, a let statement

   let (x:xs) = expr in ...

simply assumes that  expr  is of the form (x:xs) but does not force it 
and check whether that's really the case. Of course, this may turn out 
as pattern match later on as soon as  x  is demanded but  expr  was 
initially the empty list.

In your case, the test  null ss  forces  ss  and checks whether the 
let-pattern is ok. So, you basically end up doing what a case expression 
would do. In other words, the situation is more "they are most likely 
forced" than "they are probably forced" and it's just a matter of 
convenience to choose one over the other.

But there are certain situations where you can check/prove differently 
that the let pattern never fails and where such a lazy pattern is wanted.

Regards,
apfelmus



More information about the Haskell-Cafe mailing list