git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Update README.md
70bc922Phil Freeman
README.md+93−51
@@ -372,11 +372,9 @@ foo = bar(x) +
372baz(x)
372baz(x)
373```
373```
374
374
375−## Do Notation
375+## Blocks
376
376
377−There is a keyword `do`, which does not have the same purpose as the `do` keyword in Haskell.
378−
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.
380
378
381The following types of statement are supported:
379The following types of statement are supported:
382
380
@@ -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
387+- Naked expressions
389
388
390Here is an example of a power function defined using a block:
389Here is an example of a power function defined using a block:
391
390
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;
396+ }
397− return m
397+ return m;
398+ }
398```
399```
399
400
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:
403
404
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;
409+ }
408```
410```
409
411
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:
413
415
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+ }
419```
422```
420
423
421## For Loops
424## For Loops
@@ -423,11 +426,13 @@ incr = \n -> do
423For loops look like this:
426For loops look like this:
424
427
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;
433+ }
430− return n
434+ return n;
435+ }
431```
436```
432
437
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:
438
443
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;
449+ }
444− return n
450+ return n;
451+ }
445```
452```
446
453
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:
450
457
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;
465+ }
458− return count
466+ return count;
467+ }
459```
468```
460
469
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:
464
473
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;
472− else:
481+ } else {
473− m = m * 3 + 1
482+ m = m * 3 + 1;
483+ }
474− count = count + 1
484+ count = count + 1;
485+ }
475− return count
486+ return count;
487+ }
488+```
489+
490+## Naked Expressions as Statements
491+
492+Any expression with the empty type `{ }` can be used as a statement in a block. For example, method calls which return `{ }`:
493+
494+```haskell
495+example = \n m -> {
496+ console.log "Adding two numbers"
497+ return n + m;
498+ }
476```
499```
477
500
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
530+- Cons patterns
507- Record patterns
531- Record patterns
508- Named patterns
532- Named patterns
509- Guards
533- Guards
@@ -555,13 +579,31 @@ For example:
555```haskell
579```haskell
556f = \arr -> case arr of
580f = \arr -> case arr of
557 [x] -> x
581 [x] -> x
558− [x, y : xs] -> x * y + f(xs)
582+ [x, y] -> x * y + f xs
559 _ -> 0
583 _ -> 0
560```
584```
561
585
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.
563
587
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.
589+
590+## Cons Patterns
591+
592+The head and tail of a non-empty array can be matched by using a cons pattern:
593+
594+```haskell
595+add = \arr -> case arr of
596+ [] -> 0
597+ x : xs -> x + add xs
598+```
599+
600+`:` associates to the right:
601+
602+```haskell
603+addPairs = \arr -> case arr of
604+ x : y : xs -> x * y + addPairs xs
605+ _ -> 0
606+```
565
607
566## Record Patterns
608## Record Patterns
567
609
@@ -581,8 +623,8 @@ For example:
581
623
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
586 _ -> 0
628 _ -> 0
587```
629```
588
630
@@ -592,8 +634,8 @@ Named patterns bring additional names into scope when using nested patterns. Any
592
634
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
597```
639```
598
640
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
607 [] -> 0
649 [] -> 0
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
610```
652```
611
653
612## Type Synonyms
654## Type Synonyms