[Haskell-cafe] Haskell symbol ~
ryani.spam at gmail.com
Wed Aug 27 15:12:12 EDT 2008
Here is another example:
> f1 n ~(x:xs) = (n, x)
> f2 n (x:xs) = (n,x)
f1 5  = (5, error "irrefutable pattern match failure")
f2 5  = error "pattern match failure"
fst (f1 5 ) = 5
fst (f2 5 ) = error "pattern match failure"
The "~" delays the pattern match until evaluation of the variables
inside the pattern is demanded. If the variable is never demanded,
the pattern match doesn't happen.
It's especially useful for single-constructor datatypes (like pairs)
if you want the code to be more lazy.
On Wed, Aug 27, 2008 at 11:56 AM, Neil Mitchell <ndmitchell at gmail.com> wrote:
>> At the same place, I found that example,
>> but wasn't wise enough to figure out
>> what it does:
>> (f *** g) ~(x,y) = (f x, g y)
>> Can you help me understand it?
> It means exactly the same as:
> (f *** g) xy = (f (fst xy), g (snd xy))
> i.e. if you call (f *** g) undefined, you will get (f undefined, g
> undefined). If the pattern was strict (i.e. no ~) you would get
> Please update the keyword wiki so it makes sense to you, after you
> have got your head round it.
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
More information about the Haskell-Cafe