[Haskell] insufficiently laziness@pattern -- more counterintuitive stuff

Peter Thiemann thiemann at informatik.uni-freiburg.de
Tue Mar 30 18:14:06 EST 2004

>>>>> "aj" == S Alexander Jacobson <alex at alexjacobson.com> writes:

    aj> I would assume that this function:
    aj>   foo list@(h:t) = list

    aj> is equivalent to

    aj>   foo list = list
    aj>      where (h:t)=list

    aj> But passing [] to the first generates an error
    aj> even though h and t are never used!  Passing [] to
    aj> the second works just fine.

The @-pattern behaves exactly as specified in the Haskell report under
"3.17.2 Informal Semantics of Pattern Matching": 

   9. Matching an as-pattern var at apat against a value v is the result
      of matching apat against v, augmented with the binding of var to
      v. If the match of apat against v fails or diverges, then so
      does the overall match.  

And the following translation is suggested in the preceding section:

Patterns of the form var at pat are called as-patterns, and allow one to
use var as a name for the value being matched by pat. For example, 

case e of { xs@(x:rest) -> if x==0 then rest else xs }

is equivalent to:

let { xs = e } in
  case xs of { (x:rest) -> if x==0 then rest else xs }


More information about the Haskell mailing list