
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

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

On Sat, 20 Oct 2001, Adrian Hey wrote: ... : Interesting. I always thought pattern matching was lazy, but it I don't think Haskell has changed here (recently).
main = print test3
This does not work:
test1= let [a,b,c] = "Hello" in [a,b]
Same here:
test2= let a: b: c: [] = "Hello" in [a,b]
But this works - thanks to the irrefutable pattern.
test3= let a: b: ~(c: []) = "Hello" in [a,b]
This also works as variables (here cs) are irrefutable.
test4= let a: b: cs = "Hello" c: [] = cs in [a,b]
irrefutable = will always (pretend to) match (may fail later) = "lazy" match (don't match unless you have to) = delayed match (delayed to first use of some variable in the pattern) On Sat, 20 Oct 2001, Adrian Hey wrote: : Putting a ~ in front of the pattern [a,b,c] makes no difference. But the The "marker" ~ for irrefutable patterns only work "one level" down, so in ~[a,b,c] only the outermost (:) is affected. But in let the outermost pattern is lazy anyway so there is no difference. Further examples: This also works: (the (mis)match with the empty list is delayed until the end of time)
test5= let a: b: ~(c: ~[]) = "Hello" in [a,b,c]
But this fails (if the full list is requested ...)
test6= let a: b: ~(c: []) = "Hello" in [a,b,c]
/Patrik

Patrik Jansson writes:
This also works: (the (mis)match with the empty list is delayed until the end of time)
test5= let a: b: ~(c: ~[]) = "Hello" in [a,b,c] ^^^^^^^ typo! You meant [a,b].
But this fails (if the full list is requested ...)
test6= let a: b: ~(c: []) = "Hello" in [a,b,c]

On Sat, 20 Oct 2001, Richard wrote:
Patrik Jansson writes:
This also works: (the (mis)match with the empty list is delayed until the end of time)
test5= let a: b: ~(c: ~[]) = "Hello" in [a,b,c] ^^^^^^^ typo! You meant [a,b].
No, I did not! The program is tested in hugs and ghci and does what it "should". If I had used [a,b] then both test5 and test6 would work. I wanted to show an example where a variable inside an irrefutable pattern is requested. And further how the inner ~[] versus [] makes a difference. /Patrik

Hi, I think a lazy pattern is "really lazy" if reference to a part of the pattern does not cause matching of rest of the pattern. For example, \ ~((:) a ((:) b [])) -> [a] should not try to match ((:) b[]) part of the pattern, since 'a' is only referenced. It seems the above pattern should be interpreted as the following patern: \ ~((:) ~a ~((:) ~b ~[])) -> [a] Prelude> (\ ~((:) a ((:) b [])) -> [a]) [2] [ Program error: {v1282 [Num_fromInt instNum_v35 2]} Prelude> (\ ~((:) ~a ~((:) ~b ~[])) -> [a]) [2] [2] What are the cons in this interpretation? Cheers, Saswat
participants (4)
-
Adrian Hey
-
Patrik Jansson
-
Richard
-
Saswat Anand