git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Update docs
commit3059a14da5ce13f86f00b0218bf56ef3dc1b7091 authorPhil Freeman <paf31@cantab.net> dateThu 26 Dec 2013, 15:14 (13 years ago) parentfe0c18b
1 file changed+44−27
README.md+44−27
README.md+44−27
@@ -74,7 +74,7 @@ As an introductory example, here is the usual "Hello World" written in PureScrip
7474```haskell
7575foreign import console :: { log :: String -> {} }
7676
77−main = \() -> console.log "Hello, World!"
77+main = \ -> console.log "Hello, World!"
7878```
7979
8080which compiles to the following Javascript:
@@ -95,8 +95,7 @@ data Person = Person { name :: String, age :: Number }
9595foreign import numberToString :: Number -> String
9696
9797showPerson :: Person -> String
98−showPerson = \p -> case p of
99− Person o -> o.name ++ ", aged " ++ numberToString(o.age)
98+showPerson (Person o) = o.name ++ ", aged " ++ numberToString(o.age)
10099```
101100
102101Line by line, this reads as follows:
@@ -105,21 +104,21 @@ Line by line, this reads as follows:
105104 - The `Person` constructor takes an object with two properties, `name` which is a `String`, and `age` which is a `Number`
106105- The `numberToString` function is written in Javascript, and converts a `Number` to its `String` representation
107106- The `showPerson` function takes a `Person` and returns a `String`
108−- `showPerson` works by case analysis on its argument `p`, first matching the constructor `Person` and then using string concatenation and object accessors to return its result.
107+- `showPerson` works by case analysis on its argument, first matching the constructor `Person` and then using string concatenation and object accessors to return its result.
109108
110109The generated Javascript looks like this:
111110
112111```javascript
113112var Person = function (value) {
114− return { ctor: 'Person', value: value };
113+ return { ctor: 'Person', value: value };
115114};
116115
117−function showPerson(p) {
118− if (p.ctor === "Person") {
119− return p.value.name + ", aged " + numberToString(p.value.age)
120− };
121− throw "Failed pattern match"
122−}
116+function showPerson(_1) {
117+ if (_1.ctor === "Person") {
118+ return _1.value.name + ", aged " + numberToString(_1.value.age);
119+ };
120+ throw "Failed pattern match";
121+};
123122
124123```
125124
@@ -141,7 +140,7 @@ The type system defines the following types:
141140 - E.g. `Number -> String`
142141 - E.g. `(Number, Number) -> Number`
143142 - Functions can have zero or more arguments
144−- Polymorphic types (for top level declarations only)
143+- Polymorphic types
145144 - E.g. `forall a. a -> a`
146145
147146## Primitive Types
@@ -188,11 +187,10 @@ For example:
188187```haskell
189188data Foo a = Foo | Bar String
190189
191−runFoo = \foo -> case foo of
192−Foo -> "It's a Foo"
193−Bar s -> "It's a Bar. The string is " ++ s
190+runFoo Foo = "It's a Foo"
191+runFoo (Bar s) = "It's a Bar. The string is " ++ s
194192
195−runFoo Foo ++ runFoo (Bar "Test")
193+test = runFoo Foo ++ runFoo (Bar "Test")
196194```
197195
198196In the example, Foo is a tagged union type which has two constructors. It's first constructor `Foo` takes no argument, and it's second `Bar` takes one, which must be a String.
@@ -289,7 +287,7 @@ Expressions defined at the top level may have polymorphic types.
289287Here is an example:
290288
291289```haskell
292−identity = \x -> x
290+identity x = x
293291```
294292
295293`identity` is inferred to have (polymorphic) type `forall t0. t0 -> t0`. This means that for any type `t0`, `identity` can be given a value of type `t0` and will give back a value of the same type.
@@ -298,13 +296,13 @@ A type annotation can also be provided:
298296
299297```haskell
300298identity :: forall a. a -> a
301−identity = \x -> x
299+identity x = x
302300```
303301
304302Functions may also be polymorphic in row types or type variables with other kinds (see "Kind System"):
305303
306304```haskell
307−addProps = \o -> o.foo + o.bar
305+addProps o = o.foo + o.bar
308306```
309307
310308Here, `addProps` is inferred to have type `forall r. { foo :: Number, bar :: Number | r } -> Number`. That is, it can take any type which has properties `Foo` and `Bar`, and *any other record properties*.
@@ -335,7 +333,7 @@ As an example, we can pass a polymorphic function as an argument to another func
335333
336334```haskell
337335poly :: (forall a. a -> a) -> Boolean
338−poly = \f -> (f 0 < 1) == f true
336+poly f = (f 0 < 1) == f true
339337```
340338
341339Notice that the polymorphic function's type argument is instantiated to both `Number` and `Boolean`.
@@ -391,7 +389,7 @@ The following types of statement are supported:
391389Here is an example of a power function defined using a block:
392390
393391```haskell
394−pow = \n p -> {
392+pow n p = {
395393 var m = n;
396394 for (i <- 0 until p) {
397395 m = m * n;
@@ -405,7 +403,7 @@ Blocks enable local mutation of their variables, but mutation is not allowed in
405403That is, while the example above is valid, the following does not compile:
406404
407405```haskell
408−incr = \n -> {
406+incr n = {
409407 n = n + 1;
410408 return n;
411409 }
@@ -416,7 +414,7 @@ The variable `n` is not mutable, and so the assignment in the first line of the
416414This function can be rewritten as follows:
417415
418416```haskell
419−incr = \n -> {
417+incr n = {
420418 var m = n;
421419 m = m + 1;
422420 return m;
@@ -444,7 +442,7 @@ The bounds `0` and `10` are inclusive and exclusive respectively.
444442For each loops loop over the elements in an array using the `Object.forEach` method. A polyfill may be required for some browsers:
445443
446444```haskell
447−total = \arr -> {
445+total arr = {
448446 var n = 0;
449447 foreach (i in arr) {
450448 n = n + i;
@@ -458,7 +456,7 @@ total = \arr -> {
458456The syntax of a while loop is similar to a foreach loop:
459457
460458```haskell
461−log2 = \n -> {
459+log2 n = {
462460 var count = 0;
463461 var m = n;
464462 while (m > 1) {
@@ -474,7 +472,7 @@ log2 = \n -> {
474472Else branches are optional, and may contain further `if` statements, just as in Javascript:
475473
476474```haskell
477−collatz = \n -> {
475+collatz n = {
478476 var count = 0;
479477 var m = n;
480478 while (m > 1) {
@@ -494,7 +492,7 @@ collatz = \n -> {
494492Any expression with the empty type `{ }` can be used as a statement in a block. For example, method calls which return `{ }`:
495493
496494```haskell
497−example = \n m -> {
495+example n m = {
498496 console.log "Adding two numbers"
499497 return n + m;
500498 }
@@ -523,6 +521,18 @@ case value of
523521 pattern -> result
524522```
525523
524+Pattern matching can also be used in the declaration of functions, as we have already seen:
525+
526+```haskell
527+fn pattern_1 ... pattern_n = result
528+```
529+
530+Any of the above types of pattern are also valid when introducing functions. In addition, patterns can also be grouped in parentheses to introduce multiple-arugument functions. For example,
531+
532+```haskell
533+example x (y, z) w = x * y + z * w
534+```
535+
526536The following pattern types are supported:
527537
528538- Wildcard pattern
@@ -653,6 +663,13 @@ evens = \arr -> case arr of
653663 _ : xs -> evens xs
654664```
655665
666+When defining a function, guards appear after all patterns:
667+
668+```haskell
669+greater x y | x > y = true
670+greater _ _ = false
671+```
672+
656673## Type Synonyms
657674
658675For convenience, it is possible to declare a synonym for a type using the `type` keyword. Type synonyms can include type arguments.