[Haskell-beginners] Meaning of the ! operator
Daniel Fischer
daniel.is.fischer at web.de
Sun Jun 13 16:37:47 EDT 2010
On Sunday 13 June 2010 22:12:22, Nathan Huesken wrote:
> Hi,
>
> I have often seen in haskell code a "!" in front of variables.
> In example:
>
> data BrickState = Live | Dying !GLfloat deriving (Eq,Show)
>
> I never read the meaning of the "!", what does it do?
'!' is a strictness annotation.
In this case, it means that the field is strict, that is, it can't contain
an unevaluated thunk (because GLfloat is a type that is either fully
evaluated or not at all).
When you call (Dying something), the something is 'seq'ed, in particular
Dying undefined === undefined, while without the '!', Dying undefined would
be a non-bottom value.
You'll also see '!' in pattern matches,
fun !x = whatever
there it also means that the argument x must be evaluated (to weak head
normal form), so if
foo !x = 1
and
bar x = 1
, we have bar undefined = 1, but foo undefined = Prelude.error undefined
However, foo [undefined] = 1.
>
> Thanks!
> Nathan
More information about the Beginners
mailing list