Keywords

From HaskellWiki
Revision as of 18:23, 15 January 2006 by NeilMitchell (talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This page lists all Haskell keywords, feel free to edit. Hoogle searches will return results from this page. Please respect the Anchor macros.

For additional information you might want to look at the Haskell 98 report.

)

|

safeTail x | null x = []
           | otherwise = tail x
squares = [a*a | a <- [1..]]

[[Anchor(->)]]

->

Please write this

[[Anchor(<-)]]

<-

Please write this

Anchor(@)

@

Patterns of the form var@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 }

Anchor(!)

!

Whenever a data constructor is applied, each argument to the constructor is evaluated if and only if the corresponding type in the algebraic datatype declaration has a strictness flag, denoted by an exclamation point. For example:

data STList a 
        = STCons a !(STList a)  -- the second argument to STCons will be 
                                -- evaluated before STCons is applied
        | STNil

to illustrate the difference between strict versus lazy constructor application, consider the following:

stList = STCons 1 undefined
lzList = (:)    1 undefined
stHead (STCons h _) = h -- this evaluates to undefined when applied to stList
lzHead (h : _)      = h -- this evaluates to 1 when applied to lzList

Anchor(::)

::

Please write this

Anchor(_)

_

Patterns of the form _ are wildcards and are useful when some part of a pattern is not referenced on the right-hand-side. It is as if an identifier not used elsewhere were put in its place. For example,

case e of { [x,_,_]  ->  if x==0 then True else False }

is equivalent to:

case e of { [x,y,z]  ->  if x==0 then True else False }

Anchor(~)

~

Please write this

Anchor(as)

as

Please write this

Anchor(case)Anchor(of)

case, of

A case expression has the general form

case e of { p1 match1 ; ... ; pn matchn }

where each match is of the general form

 | g1  -> e1
   ...
 | gm -> em
    where decls

Each alternative consists of a pattern pi and its matches, matchi. Each match in turn consists of a sequence of pairs of guards gj and bodies ej (expressions), followed by optional bindings (decls) that scope over all of the guards and expressions of the alternative. An alternative of the form

pat -> exp where decls

is treated as shorthand for:

 pat | True -> exp
 where decls

A case expression must have at least one alternative and each alternative must have at least one body. Each body must have the same type, and the type of the whole expression is that type.

A case expression is evaluated by pattern matching the expression e against the individual alternatives. The alternatives are tried sequentially, from top to bottom. If e matches the pattern in the alternative, the guards for that alternative are tried sequentially from top to bottom, in the environment of the case expression extended first by the bindings created during the matching of the pattern, and then by the declsi in the where clause associated with that alternative. If one of the guards evaluates to True, the corresponding right-hand side is evaluated in the same environment as the guard. If all the guards evaluate to False, matching continues with the next alternative. If no match succeeds, the result is _|_.

Anchor(class)

class

Please write this

Anchor(data)

data

Please write this

Anchor(default)

default

Please write this

Anchor(deriving)

deriving

Please write this

Anchor(do)

do

Please write this

Anchor(forall)

forall

This is a GHC/Hugs extension, and as such is not portable Haskell 98.

Anchor(hiding)

hiding

Please write this

Anchor(if)Anchor(then)Anchor(else)

if, then, else

A conditional expression has the form:

if e1 then e2 else e3

...and returns the value of e2 if the value of e1 is True, e3 if e1 is False, and _|_ otherwise.

max a b = if a > b then a else b

Anchor(import)

import

Please write this

Anchor(infix)Anchor(infixr)Anchor(infixl)

infix, infixl, infixr

Please write this

Anchor(instance)

instance

Please write this

Anchor(let)Anchor(in)

let, in

Let expressions have the general form:

let { d1 ; ... ; dn } in e

They introduce a nested, lexically-scoped, mutually-recursive list of declarations (let is often called letrec in other languages). The scope of the declarations is the expression e and the right hand side of the declarations.

Anchor(module)

module

Please write this

Anchor(newtype)

newtype

Please write this

Anchor(qualified)

qualified

Please write this

Anchor(type)

type

Please write this

Anchor(where)

where

Please write this