git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Update README.md
commit70bc9224e34050ba2610cf123eb0fe62f68d9d4e authorPhil Freeman <paf31@cantab.net> dateSun 22 Dec 2013, 17:29 (13 years ago) parentc8778d3
1 file changed+93−51
README.md+93−51
README.md+93−51
@@ -372,11 +372,9 @@ foo = bar(x) +
372372baz(x)
373373```
374374
375−## Do Notation
375+## Blocks
376376
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.
380378
381379The following types of statement are supported:
382380
@@ -386,15 +384,18 @@ The following types of statement are supported:
386384- For-each loops
387385- While loops
388386- If-Then-Else statements
387+- Naked expressions
389388
390389Here is an example of a power function defined using a block:
391390
392391```haskell
393−pow = \n p -> do
394− var m = n
395− for i <- 0 until p:
396− m = m * n
397− return m
392+pow = \n p -> {
393+ var m = n;
394+ for (i <- 0 until p) {
395+ m = m * n;
396+ }
397+ return m;
398+ }
398399```
399400
400401Blocks 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
402403That is, while the example above is valid, the following does not compile:
403404
404405```haskell
405−incr = \n -> do
406− n = n + 1
407− return n
406+incr = \n -> {
407+ n = n + 1;
408+ return n;
409+ }
408410```
409411
410412The 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
412414This function can be rewritten as follows:
413415
414416```haskell
415−incr = \n -> do
416− var m = n
417− m = m + 1
418− return m
417+incr = \n -> {
418+ var m = n;
419+ m = m + 1;
420+ return m;
421+ }
419422```
420423
421424## For Loops
@@ -423,11 +426,13 @@ incr = \n -> do
423426For loops look like this:
424427
425428```haskell
426−total = do
427− var n = 0
428− for i <- 0 until 10:
429− n = n + i
430− return n
429+total = {
430+ var n = 0;
431+ for (i <- 0 until 10) {
432+ n = n + i;
433+ }
434+ return n;
435+ }
431436```
432437
433438The bounds `0` and `10` are inclusive and exclusive respectively.
@@ -437,11 +442,13 @@ The bounds `0` and `10` are inclusive and exclusive respectively.
437442For each loops loop over the elements in an array using the `Object.forEach` method. A polyfill may be required for some browsers:
438443
439444```haskell
440−total = \arr -> do
441− var n = 0
442− foreach i in arr:
443− n = n + i
444− return n
445+total = \arr -> {
446+ var n = 0;
447+ foreach (i in arr) {
448+ n = n + i;
449+ }
450+ return n;
451+ }
445452```
446453
447454## While Loops
@@ -449,13 +456,15 @@ total = \arr -> do
449456The syntax of a while loop is similar to a foreach loop:
450457
451458```haskell
452−log2 = \n -> do
453− var count = 0
454− var m = n
455− while m > 1:
456− m = m / 2
457− count = count + 1
458− return count
459+log2 = \n -> {
460+ var count = 0;
461+ var m = n;
462+ while (m > 1) {
463+ m = m / 2;
464+ count = count + 1;
465+ }
466+ return count;
467+ }
459468```
460469
461470## If-Then-Else Statements
@@ -463,16 +472,30 @@ log2 = \n -> do
463472Else branches are optional, and may contain further `if` statements, just as in Javascript:
464473
465474```haskell
466−collatz = \n -> do
467− var count = 0
468− var m = n
469− while m > 1:
470− if m % 2 == 0:
471− m = m / 2
472− else:
473− m = m * 3 + 1
474− count = count + 1
475− return count
475+collatz = \n -> {
476+ var count = 0;
477+ var m = n;
478+ while (m > 1) {
479+ if (m % 2 == 0) {
480+ m = m / 2;
481+ } else {
482+ m = m * 3 + 1;
483+ }
484+ count = count + 1;
485+ }
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+ }
476499```
477500
478501## If-Then-Else Expressions
@@ -504,6 +527,7 @@ The following pattern types are supported:
504527- Literal patterns
505528- Variable pattern
506529- Array patterns
530+- Cons patterns
507531- Record patterns
508532- Named patterns
509533- Guards
@@ -555,13 +579,31 @@ For example:
555579```haskell
556580f = \arr -> case arr of
557581 [x] -> x
558− [x, y : xs] -> x * y + f(xs)
582+ [x, y] -> x * y + f xs
559583 _ -> 0
560584```
561585
562586Here, the first pattern only matches arrays of length one, and brings the first element of the array into scope.
563587
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+```
565607
566608## Record Patterns
567609
@@ -581,8 +623,8 @@ For example:
581623
582624```haskell
583625f = \o -> case o of
584− { arr = [x:_], take = "car" } -> x
585− { arr = [_,x:_], take = "cadr" } -> x
626+ { arr = x : _, take = "car" } -> x
627+ { arr = _ : x : _, take = "cadr" } -> x
586628 _ -> 0
587629```
588630
@@ -592,8 +634,8 @@ Named patterns bring additional names into scope when using nested patterns. Any
592634
593635```haskell
594636f = \arr -> case arr of
595− a@[_,_:_] -> a
596− a -> a
637+ a@(_ : _ : _) -> true
638+ a -> false
597639```
598640
599641Here, 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-
605647```haskell
606648evens = \arr -> case arr of
607649 [] -> 0
608− [x:xs] | x % 2 == 0 -> 1 + evens xs
609− [_:xs] -> evens xs
650+ x : xs | x % 2 == 0 -> 1 + evens xs
651+ _ : xs -> evens xs
610652```
611653
612654## Type Synonyms