https://wiki.haskell.org/api.php?action=feedcontributions&user=Lokadin&feedformat=atomHaskellWiki - User contributions [en]2021-06-16T18:25:11ZUser contributionsMediaWiki 1.27.4https://wiki.haskell.org/index.php?title=Keywords&diff=4081Keywords2006-05-12T12:12:29Z<p>Lokadin: add a chunk to modules</p>
<hr />
<div>This page lists all Haskell keywords, feel free to edit. [[Hoogle]] searches will return results from this page. Please respect the Anchor macros.<br />
<br />
For additional information you might want to look at [http://www.haskell.org/onlinereport/ the Haskell 98 report].<br />
<br />
== | ==<br />
<br />
safeTail x | null x = []<br />
| otherwise = tail x<br />
<br />
squares = [a*a | a <- [1..]]<br />
<br />
== -> ==<br />
<br />
Please write this<br />
<br />
== <- ==<br />
<br />
Please write this<br />
<br />
== @ ==<br />
<br />
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:<br />
<br />
case e of { xs@(x:rest) -> if x==0 then rest else xs }<br />
<br />
is equivalent to:<br />
<br />
let { xs = e } in<br />
case xs of { (x:rest) -> if x==0 then rest else xs }<br />
<br />
== ! ==<br />
<br />
Whenever a data [[constructor]] is applied, each argument to the constructor is evaluated if and only if the corresponding type in the algebraic data[[type]] declaration has a strictness flag, denoted by an exclamation point. For example:<br />
<br />
data STList a <br />
= STCons a !(STList a) -- the second argument to STCons will be <br />
-- evaluated before STCons is applied<br />
| STNil<br />
<br />
to illustrate the difference between strict versus lazy constructor application, consider the following:<br />
<br />
stList = STCons 1 undefined<br />
lzList = (:) 1 undefined<br />
stHead (STCons h _) = h -- this evaluates to undefined when applied to stList<br />
lzHead (h : _) = h -- this evaluates to 1 when applied to lzList<br />
<br />
== :: ==<br />
<br />
Please write this<br />
<br />
== _ ==<br />
<br />
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,<br />
<br />
case e of { [x,_,_] -> if x==0 then True else False }<br />
<br />
is equivalent to:<br />
<br />
case e of { [x,y,z] -> if x==0 then True else False }<br />
<br />
== ~ ==<br />
<br />
Please write this<br />
<br />
== as ==<br />
<br />
Please write this<br />
<br />
== case, of ==<br />
<br />
A case expression has the general form<br />
<br />
case e of { p1 match1 ; ... ; pn matchn }<br />
<br />
where each match is of the general form<br />
<br />
| g1 -> e1<br />
...<br />
| gm -> em<br />
where decls<br />
<br />
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<br />
<br />
pat -> exp where decls<br />
<br />
is treated as shorthand for:<br />
<br />
pat | True -> exp<br />
where decls<br />
<br />
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.<br />
<br />
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 _|_. <br />
<br />
== class ==<br />
<br />
Please write this<br />
<br />
== data ==<br />
<br />
The <code>data</code> declaration is how one introduces new algebraic data [[type]]s into Haskell. As an example, to create a datatype to hold an [[Abstract syntax tree]] for an expression, one could use:<br />
<br />
data Exp = Ebin Operator Exp Exp |<br />
Eunary Operator Exp |<br />
Efun FunctionIdentifier [Exp] |<br />
Eid SimpleIdentifier<br />
<br />
where the [[type]]s <code>Operator, FunctionIdentifier</code> and <code>SimpleIdentifier</code> are defined elsewhere. <br />
<br />
See the page on [[type]]s for more information, links and examples.<br />
<br />
== default ==<br />
<br />
Please write this<br />
<br />
== deriving ==<br />
<br />
Please write this<br />
<br />
== do ==<br />
<br />
Syntactic sugar for use with monadic expressions. For example:<br />
<br />
do { x ; result <- y ; foo result }<br />
<br />
is shorthand for:<br />
<br />
x >> <br />
y >>= \result -><br />
foo result<br />
<br />
== forall ==<br />
<br />
This is a GHC/Hugs extension, and as such is not portable Haskell 98.<br />
<br />
== hiding ==<br />
<br />
Please write this<br />
<br />
== if, then, else ==<br />
<br />
A conditional expression has the form:<br />
<br />
if e1 then e2 else e3<br />
<br />
...and returns the value of e2 if the value of e1 is True, e3 if e1 is False, and _|_ otherwise.<br />
<br />
max a b = if a > b then a else b<br />
<br />
== import ==<br />
<br />
Please write this<br />
<br />
== infix, infixl, infixr ==<br />
<br />
Please write this<br />
<br />
== instance ==<br />
<br />
Please write this<br />
<br />
== let, in ==<br />
<br />
Let expressions have the general form:<br />
<br />
let { d1 ; ... ; dn } in e<br />
<br />
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.<br />
<br />
== module ==<br />
<br />
taken from: http://www.haskell.org/tutorial/modules.html<br />
<br />
Technically speaking, a module is really just one big declaration which begins with the keyword module; here's an example for a module whose name is Tree:<br />
<br />
module Tree ( Tree(Leaf,Branch), fringe ) where<br />
<br />
data Tree a = Leaf a | Branch (Tree a) (Tree a) <br />
<br />
fringe :: Tree a -> [a]<br />
fringe (Leaf x) = [x]<br />
fringe (Branch left right) = fringe left ++ fringe right<br />
<br />
<br />
== newtype ==<br />
<br />
The <code>newtype</code> declaration is how one introduces a renaming for an algebraic data [[type]] into Haskell. This is different from <code>type</code> below, as a <code>newtype</code> requires a new [[constructor]] as well. As an example, when writing a compiler<br />
one sometimes further qualifies <code>Identifier</code>s to assist in type safety checks:<br />
<br />
newtype SimpleIdentifier = SimpleIdentifier Identifier<br />
newtype FunctionIdentifier = FunctionIdentifier Identifier<br />
<br />
Most often, one supplies [[smart constructors]] and [[destructor]]s for these to ease working with them.<br />
<br />
See the page on [[type]]s for more information, links and examples.<br />
<br />
== qualified ==<br />
<br />
Please write this<br />
<br />
== type ==<br />
<br />
The <code>type</code> declaration is how one introduces an alias for an algebraic data [[type]] into Haskell. As an example, when writing a compiler<br />
one often creates an alias for identifiers:<br />
<br />
type Identifier = String<br />
<br />
This allows you to use <code>Identifer</code> wherever you had used <code>String</code> and if something is of type <code>Identifier</code> it<br />
may be used wherever a <code>String</code> is expected.<br />
<br />
See the page on [[type]]s for more information, links and examples.<br />
<br />
== where ==<br />
<br />
Please write this<br />
<br />
<br />
[[Category:Language]]</div>Lokadin