A small doubt
Patrik Jansson
patrikj@cs.chalmers.se
Sat, 20 Oct 2001 17:48:30 +0200 (MET DST)
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