@@ -1,231 +1,107 @@
1−## Whitespace
1+Syntax
2+======
23
3−Syntax is whitespace sensitive. The general rule of thumb is that declarations which span multiple lines should be indented past the column on which they were first defined on their subsequent lines.
4−
5−That is, the following is valid:
6−
7−```haskell
8−foo = bar(x) +
9− baz(x)
10−```
11−
12−But this is not:
13−
14−```haskell
15−foo = bar(x) +
16−baz(x)
17−```
18−
19−## Blocks
20−
21−Blocks are collections of statements wrapped in braces `{ ... }`. Blocks must return a value of the same type on every branch of execution.
22−
23−The following types of statement are supported:
24−
25−- Variable introduction
26−- Variable assignment
27−- For loops
28−- For-each loops
29−- While loops
30−- If-Then-Else statements
31−
32−Here is an example of a power function defined using a block:
33−
34−```haskell
35−pow n p = {
36− var m = n;
37− for (i <- 0 until p) {
38− m = m * n;
39− }
40− return m;
41− }
42−```
43−
44−Blocks enable local mutation of their variables, but mutation is not allowed in general. The type system prevents mutable variables from escaping their scope.
45−
46−That is, while the example above is valid, the following does not compile:
4+Whitespace Rules
5+----------------
476
48−```haskell
49−incr n = {
50− n = n + 1;
51− return n;
52− }
53−```
54−
55−The variable `n` is not mutable, and so the assignment in the first line of the `do` block is not allowed.
56−
57−This function can be rewritten as follows:
58−
59−```haskell
60−incr n = {
61− var m = n;
62− m = m + 1;
63− return m;
64− }
65−```
7+Syntax is whitespace sensitive. The general rule of thumb is that declarations which span multiple lines should be indented past the column on which they were first defined on their subsequent lines.
668
67−## For Loops
9+That is, the following is valid::
6810
69−For loops look like this:
11+ foo = bar(x) +
12+ baz(x)
7013
71−```haskell
72−total = {
73− var n = 0;
74− for (i <- 0 until 10) {
75− n = n + i;
76− }
77− return n;
78− }
79−```
14+But this is not::
8015
81−The bounds `0` and `10` are inclusive and exclusive respectively.
82−
83−## For-Each Loops
84−
85−For each loops loop over the elements in an array using the `Object.forEach` method. A polyfill may be required for some browsers:
86−
87−```haskell
88−total arr = {
89− var n = 0;
90− foreach (i in arr) {
91− n = n + i;
92− }
93− return n;
94− }
95−```
96−
97−## While Loops
98−
99−The syntax of a while loop is similar to a foreach loop:
100−
101−```haskell
102−log2 n = {
103− var count = 0;
104− var m = n;
105− while (m > 1) {
106− m = m / 2;
107− count = count + 1;
108− }
109− return count;
110− }
111−```
112−
113−## If-Then-Else Statements
114−
115−Else branches are optional, and may contain further `if` statements, just as in Javascript:
116−
117−```haskell
118−collatz n = {
119− var count = 0;
120− var m = n;
121− while (m > 1) {
122− if (m % 2 == 0) {
123− m = m / 2;
124− } else {
125− m = m * 3 + 1;
126− }
127− count = count + 1;
128− }
129− return count;
130− }
131−```
16+ foo = bar(x) +
17+ baz(x)
13218
133−## If-Then-Else Expressions
19+If-Then-Else Expressions
20+------------------------
13421
135−The `if`, `then` and `else` keywords can also be used to create conditional expressions. In this case, the `else` block is always required.
22+The `if`, `then` and `else` keywords can be used to create conditional expressions. In this case, the `else` block is always required.
13623
137−For example,
24+For example::
13825
139−```haskell
140−conditional = if 2 > 1 then "ok" else "oops"
141−```
26+ conditional = if 2 > 1 then "ok" else "oops"
14227
143−## Do Notation
28+Do Notation
29+-----------
14430
14531The `do` keyword introduces simple syntactic sugar for monadic expressions.
14632
147−Here is an example, using the maybe monad:
148−
149−```haskell
150−data Maybe a = Nothing | Just a
151−
152−instance Monad Maybe where
153− ret = Just
154− (>>=) Nothing _ = Nothing
155− (>>=) (Just a) f = f a
156−
157−isEven n | n % 2 == 0 = Just {}
158−isEven _ = Nothing
159−
160−evenSum a b = do
161− n <- a
162− m <- b
163− let sum = n + m
164− isEven sum
165− ret sum
166−```
33+Here is an example, using the maybe monad::
34+
35+ data Maybe a = Nothing | Just a
36+
37+ instance Monad Maybe where
38+ ret = Just
39+ (>>=) Nothing _ = Nothing
40+ (>>=) (Just a) f = f a
41+
42+ isEven n | n % 2 == 0 = Just {}
43+ isEven _ = Nothing
44+
45+ evenSum a b = do
46+ n <- a
47+ m <- b
48+ let sum = n + m
49+ isEven sum
50+ ret sum
16751
16852`isEven` adds two values of type `Maybe Number` and returns their sum, if the sum is even. If the sum is odd, `evenSum` returns `Nothing`.
16953
170−This example illustrates the following aspects of `do` notation:
54+When using `do` notation, the corresponding type constructor must be an instance of the `Prelude.Monad` type class, which defines the `ret` and `>>=` functions.
17155
172−- The corresponding type constructor must be an instance of the `Prelude.Monad` type class, which defines the `ret` and `>>=` functions.
173−- Statements can have the following form:
174− - `a <- x` which desugars to `m.bind x (\a -> ...)`
175− - `let a = x` which desugars to `(\a -> ...)(x)`
176− - `x` which desugars to `m.bind x (\_ -> ...)` or just `x` if this is the last statement.
56+Statements can have the following form:
17757
178−Not illustrated here, but equally valid is the use of a binder on the left hand side of `<-` or `=`. For example:
58+- `a <- x` which desugars to `m.bind x (\a -> ...)`
59+- `let a = x` which desugars to `(\a -> ...)(x)`
60+- `x` which desugars to `m.bind x (\_ -> ...)` or just `x` if this is the last statement.
17961
180−```haskell
181−test arr = do
182− (x:y:_) <- arr
183− ret (x + y)
184−```
62+Binders can be used on the left hand side of `<-` or `=`. For example::
18563
186−A pattern match failure will generate a runtime exception, just as in the case of a regular `case` statement.
64+ test arr = do
65+ (x:y:_) <- arr
66+ ret (x + y)
18767
188−## Operators
189−
190−In addition to the standard operators, user-defined infix operators can be created by enclosing names in parentheses:
68+A pattern match failure will generate a runtime exception, just as in the case of a regular `case` statement.
19169
192−E.g. to create a synonym for string concatenation:
70+Operators
71+---------
19372
194−```haskell
195−(<>) = \s1 s2 -> s1 ++ s2
73+In addition to the standard operators, user-defined infix operators can be created by enclosing names in parentheses.
19674
197−greeting = "Hello" <> "World!"
198−```
75+E.g. to create a synonym for string concatenation::
19976
200−Regular functions can be used as operators by enclosing their names in backticks:
77+ (<>) = \s1 s2 -> s1 ++ s2
78+
79+ greeting = "Hello" <> "World!"
20180
202−```haskell
203−foo = \x y -> x * y + y
81+Regular functions can be used as operators by enclosing their names in backticks::
20482
205−test = 10 `foo` 20
206−```
83+ foo = \x y -> x * y + y
84+
85+ test = 10 `foo` 20
20786
208−Fixity declarations can associate a precedence level, which is a natural number, to a user-defined operator, and specify which way it associates:
87+Fixity declarations can associate a precedence level, which is a natural number, to a user-defined operator, and specify which way it associates::
88+
89+ infixl 5 <>
90+ infixr 7 %%
20991
210−```haskell
211−infixl 5 <>
212−infixr 7 %%
213−```
92+Record Updates
93+--------------
21494
215−## Record Updates
95+Properties on records can be updated using the following syntax::
21696
217−Properties on records can be updated using the following syntax: `o { key = value, ..., key = value }`
97+ o { key = value, ..., key = value }
21898
219−For example, the following function increments the `foo` property on its argument:
99+For example, the following function increments the `foo` property on its argument::
220100
221−```haskell
222−incr = \o -> o { foo = o.foo + 1 }
223−```
101+ incr = \o -> o { foo = o.foo + 1 }
224102
225−The generated Javascript assumes the existence of a method called `Object.extend` such that `Object.extend(o, p)` takes an object `o` and generates a shallow copy of `o` including the properties of `p`. A simple JQuery implementation of this specification is
103+The generated Javascript assumes the existence of a method called `Object.extend` such that `Object.extend(o, p)` takes an object `o` and generates a shallow copy of `o` including the properties of `p`. A simple JQuery implementation of this specification is::
226104
227−```haskell
228105Object.prototype.extend = function(o, p) {
229106 return $.extend({}, o, p);
230107};
231−```