A small doubt

Adrian Hey ahey@iee.org
Sat, 20 Oct 2001 15:43:58 +0100


On Saturday 20 October 2001  3:58 am, Saswat Anand wrote:
> Hi,
>   I am wondering why this function should not work with input [x,y]
> (list with two elements) too, since third element is not referenced. Why
> is it so eager to pattern match.
>
> fun = \list -> let [a,b,c] = list
> 		in [a,b]
>
>
> Thanks,
> Saswat

Interesting. I always thought pattern matching was lazy, but it
seems it's not quite as lazy as I thought. With GHC..

main = let [a,b,c] = "Hello"
       in putStrLn [a,b]

fails with a message about irrefutable patterns, but this seems
wrong to me because in order to determine this the program has done
more reductions than are necessary to determine values of a and b.
Putting a ~ in front of the pattern makes no difference. But the
apparently equivalent form..

main = let (a:as) = "Hello"
           (b:bs) = as
           [c]    = bs
       in putStrLn [a,b]
succeeds. ??

Mind you, now I've re-read the stuff in the Haskell 98 report
about this I'm not at all sure I understand it properly. Has
the language changed in this respect? I seem to remember a
more comprehensible explanation of refutable and irrefutable
patterns and the effect of ~ in previous versions.

Regards
--
Adrian Hey