@@ -372,11 +372,9 @@ foo = bar(x) +
375−## Do Notation
375+## Blocks
377−There is a keyword `do`, which does not have the same purpose as the `do` keyword in Haskell.
379−`do` introduces a block. Blocks consist of statements, and must return a value of the same type on every branch of execution.
377+Blocks are collections of statements wrapped in braces `{ ... }`. Blocks must return a value of the same type on every branch of execution.
381The following types of statement are supported:
379The following types of statement are supported:
@@ -386,15 +384,18 @@ The following types of statement are supported:
386- For-each loops
384- For-each loops
387- While loops
385- While loops
388- If-Then-Else statements
386- If-Then-Else statements
390Here is an example of a power function defined using a block:
389Here is an example of a power function defined using a block:
392```haskell
391```haskell
393−pow = \n p -> do
392+pow = \n p -> {
394− var m = n
393+ var m = n;
395− for i <- 0 until p:
394+ for (i <- 0 until p) {
396− m = m * n
395+ m = m * n;
397− return m
397+ return m;
400Blocks enable local mutation of their variables, but mutation is not allowed in general. The type system prevents mutable variables from escaping their scope.
401Blocks enable local mutation of their variables, but mutation is not allowed in general. The type system prevents mutable variables from escaping their scope.
@@ -402,9 +403,10 @@ Blocks enable local mutation of their variables, but mutation is not allowed in
402That is, while the example above is valid, the following does not compile:
403That is, while the example above is valid, the following does not compile:
404```haskell
405```haskell
405−incr = \n -> do
406+incr = \n -> {
406− n = n + 1
407+ n = n + 1;
407− return n
408+ return n;
410The variable `n` is not mutable, and so the assignment in the first line of the `do` block is not allowed.
412The variable `n` is not mutable, and so the assignment in the first line of the `do` block is not allowed.
@@ -412,10 +414,11 @@ The variable `n` is not mutable, and so the assignment in the first line of the
412This function can be rewritten as follows:
414This function can be rewritten as follows:
414```haskell
416```haskell
415−incr = \n -> do
417+incr = \n -> {
416− var m = n
418+ var m = n;
417− m = m + 1
419+ m = m + 1;
418− return m
420+ return m;
421## For Loops
424## For Loops
@@ -423,11 +426,13 @@ incr = \n -> do
423For loops look like this:
426For loops look like this:
425```haskell
428```haskell
426−total = do
429+total = {
427− var n = 0
430+ var n = 0;
428− for i <- 0 until 10:
431+ for (i <- 0 until 10) {
429− n = n + i
432+ n = n + i;
430− return n
434+ return n;
433The bounds `0` and `10` are inclusive and exclusive respectively.
438The bounds `0` and `10` are inclusive and exclusive respectively.
@@ -437,11 +442,13 @@ The bounds `0` and `10` are inclusive and exclusive respectively.
437For each loops loop over the elements in an array using the `Object.forEach` method. A polyfill may be required for some browsers:
442For each loops loop over the elements in an array using the `Object.forEach` method. A polyfill may be required for some browsers:
439```haskell
444```haskell
440−total = \arr -> do
445+total = \arr -> {
441− var n = 0
446+ var n = 0;
442− foreach i in arr:
447+ foreach (i in arr) {
443− n = n + i
448+ n = n + i;
444− return n
450+ return n;
447## While Loops
454## While Loops
@@ -449,13 +456,15 @@ total = \arr -> do
449The syntax of a while loop is similar to a foreach loop:
456The syntax of a while loop is similar to a foreach loop:
451```haskell
458```haskell
452−log2 = \n -> do
459+log2 = \n -> {
453− var count = 0
460+ var count = 0;
454− var m = n
461+ var m = n;
455− while m > 1:
462+ while (m > 1) {
456− m = m / 2
463+ m = m / 2;
457− count = count + 1
464+ count = count + 1;
458− return count
466+ return count;
461## If-Then-Else Statements
470## If-Then-Else Statements
@@ -463,16 +472,30 @@ log2 = \n -> do
463Else branches are optional, and may contain further `if` statements, just as in Javascript:
472Else branches are optional, and may contain further `if` statements, just as in Javascript:
465```haskell
474```haskell
466−collatz = \n -> do
475+collatz = \n -> {
467− var count = 0
476+ var count = 0;
468− var m = n
477+ var m = n;
469− while m > 1:
478+ while (m > 1) {
470− if m % 2 == 0:
479+ if (m % 2 == 0) {
471− m = m / 2
480+ m = m / 2;
473− m = m * 3 + 1
482+ m = m * 3 + 1;
474− count = count + 1
484+ count = count + 1;
475− return count
486+ return count;
490+## Naked Expressions as Statements
492+Any expression with the empty type `{ }` can be used as a statement in a block. For example, method calls which return `{ }`:
496+ console.log "Adding two numbers"
478## If-Then-Else Expressions
501## If-Then-Else Expressions
@@ -504,6 +527,7 @@ The following pattern types are supported:
504- Literal patterns
527- Literal patterns
505- Variable pattern
528- Variable pattern
506- Array patterns
529- Array patterns
507- Record patterns
531- Record patterns
508- Named patterns
532- Named patterns
@@ -555,13 +579,31 @@ For example:
555```haskell
579```haskell
556f = \arr -> case arr of
580f = \arr -> case arr of
558− [x, y : xs] -> x * y + f(xs)
582+ [x, y] -> x * y + f xs
562Here, the first pattern only matches arrays of length one, and brings the first element of the array into scope.
586Here, the first pattern only matches arrays of length one, and brings the first element of the array into scope.
564−The second pattern matches arrays with two or more elements, and brings the first and second elements into scope, along with the remainder of the array as the variable `xs`.
588+The second pattern matches arrays with two elements, and brings the first and second elements into scope.
592+The head and tail of a non-empty array can be matched by using a cons pattern:
595+add = \arr -> case arr of
597+ x : xs -> x + add xs
600+`:` associates to the right:
603+addPairs = \arr -> case arr of
604+ x : y : xs -> x * y + addPairs xs
566## Record Patterns
608## Record Patterns
@@ -581,8 +623,8 @@ For example:
582```haskell
624```haskell
583f = \o -> case o of
625f = \o -> case o of
584− { arr = [x:_], take = "car" } -> x
626+ { arr = x : _, take = "car" } -> x
585− { arr = [_,x:_], take = "cadr" } -> x
627+ { arr = _ : x : _, take = "cadr" } -> x
@@ -592,8 +634,8 @@ Named patterns bring additional names into scope when using nested patterns. Any
593```haskell
635```haskell
594f = \arr -> case arr of
636f = \arr -> case arr of
595− a@[_,_:_] -> a
637+ a@(_ : _ : _) -> true
596− a -> a
638+ a -> false
599Here, in the first pattern, any array with two or more elements will be matched and bound to the variable `a`.
641Here, in the first pattern, any array with two or more elements will be matched and bound to the variable `a`.
@@ -605,8 +647,8 @@ Guards are used to impose additional constraints inside a pattern using boolean-
605```haskell
647```haskell
606evens = \arr -> case arr of
648evens = \arr -> case arr of
608− [x:xs] | x % 2 == 0 -> 1 + evens xs
650+ x : xs | x % 2 == 0 -> 1 + evens xs
609− [_:xs] -> evens xs
651+ _ : xs -> evens xs
612## Type Synonyms
654## Type Synonyms