git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Update docs for multiple argument functions
commit65e05dc8b6d0f2559af9e926c764ffcee4442128 authorPhil Freeman <paf31@cantab.net> dateSun 2 Feb 2014, 16:42 (13 years ago) parent571af42
6 files changed+52−105
docs/source/blocks.rst+4−4 docs/source/intro.rst−1 docs/source/modules.rst+2−2 docs/source/patterns.rst+29−41 docs/source/syntax.rst+16−56 docs/source/types.rst+1−1
docs/source/blocks.rst+4−4
@@ -1,7 +1,7 @@
11Blocks
22======
33
4−Blocks are collections of statements wrapped in braces `{ ... }`. Blocks must return a value of the same type on every branch of execution.
4+Blocks are collections of statements wrapped in braces ``{ ... }``. Blocks must return a value of the same type on every branch of execution.
55
66The following types of statement are supported:
77
@@ -30,7 +30,7 @@ That is, while the example above is valid, the following does not compile::
3030 return n;
3131 }
3232
33−The variable `n` is not mutable, and so the assignment in the first line of the `do` block is not allowed.
33+The variable ``n`` is not mutable, and so the assignment in the first line of the block is not allowed.
3434
3535This function can be rewritten as follows::
3636
@@ -58,7 +58,7 @@ The bounds `0` and `10` are inclusive and exclusive respectively.
5858While Loops
5959------------
6060
61−While loops repeat a set of statements while a boolean expression evaluates to `true`::
61+While loops repeat a set of statements while a boolean expression evaluates to ``true``::
6262
6363 log2 n = {
6464 var count = 0;
@@ -73,7 +73,7 @@ While loops repeat a set of statements while a boolean expression evaluates to `
7373If-Then-Else Statements
7474-----------------------
7575
76−Else branches are optional, and may contain further `if` statements, just as in Javascript:
76+Else branches are optional, and may contain further ``if`` statements, just as in Javascript:
7777
7878 collatz n = {
7979 var count = 0;
docs/source/intro.rst−1
@@ -9,7 +9,6 @@ PureScript is a small strongly, statically typed compile-to-JS language with a n
99- Extensible records
1010- Extensible effects
1111- Type-safe blocks with for/while/assignment etc.
12−- Functions with multiple arguments
1312- Pattern matching
1413- Simple FFI
1514- Modules
docs/source/modules.rst+2−2
@@ -1,7 +1,7 @@
11Modules
22=======
33
4−All code in PureScript is contained in a module. Modules are introduced using the `module` keyword::
4+All code in PureScript is contained in a module. Modules are introduced using the ``module`` keyword::
55
66 module A where
77
@@ -16,7 +16,7 @@ When referencing values or data types in another module, names may be qualified
1616Importing Modules
1717-----------------
1818
19−A module can be imported using the `import` keyword. This will create aliases for all of the values and types in the imported module::
19+A module can be imported using the ``import`` keyword. This will create aliases for all of the values and types in the imported module::
2020
2121 module B where
2222
docs/source/patterns.rst+29−41
@@ -14,9 +14,9 @@ Pattern matching can also be used in the declaration of functions, as we have al
1414
1515 fn pattern_1 ... pattern_n = result
1616
17−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::
17+Patterns can also be used when introducing functions. For example::
1818
19− example x (y, z) w = x * y + z * w
19+ example x y z = x * y + z
2020
2121The following pattern types are supported:
2222
@@ -36,43 +36,37 @@ Wildcard Patterns
3636
3737The wilcard `_` matches any input and brings nothing into scope::
3838
39− case x of
40− _ -> 0
39+ f _ = 0
4140
4241Literal Patterns
4342----------------
4443
4544Literal patterns are provided to match on primitives::
4645
47− case booleanValue of
48− true -> 0
49− false -> 1
46+ f true = 0
47+ f false = 1
5048
51− case stringValue of
52− "Foo" -> 0
53− _ -> 1
49+ g "Foo" = 0
50+ g _ = 1
5451
55− case numericValue of
56− 0 -> 0
57− _ -> 1
52+ h 0 = 0
53+ h _ = 1
5854
5955Variable Patterns
6056-----------------
6157
6258A variable pattern matches any input and binds that input to its name::
6359
64− case foo(x) of
65− result -> bar(result)
60+ double x = x * 2
6661
6762Array Patterns
6863--------------
6964
7065Array patterns match an input which is an array, and bring its elements into scope. For example::
7166
72− f = \arr -> case arr of
73− [x] -> x
74− [x, y] -> x * y + f xs
75− _ -> 0
67+ f [x] = x
68+ f [x, y] = x * y + f xs
69+ f _ = 0
7670
7771Here, the first pattern only matches arrays of length one, and brings the first element of the array into scope.
7872
@@ -83,55 +77,49 @@ Cons Patterns
8377
8478The head and tail of a non-empty array can be matched by using a cons pattern::
8579
86− add = \arr -> case arr of
87− [] -> 0
88− x : xs -> x + add xs
80+ sum [] = 0
81+ sum (x : xs) = x + sum xs
8982
9083`:` associates to the right::
9184
92− addPairs = \arr -> case arr of
93− x : y : xs -> x * y + addPairs xs
94− _ -> 0
85+ addPairs (x : y : xs) = x * y + addPairs xs
86+ addPairs _ = 0
9587
9688Record Patterns
9789---------------
9890
9991Record patterns match an input which is a record, and bring its properties into scope::
10092
101− f = \o -> case o of
102− { foo = "Foo" } -> o.bar
103− _ -> 0
93+ f { foo = "Foo" } = o.bar
94+ f _ = 0
10495
10596Nested Patterns
10697---------------
10798
10899The patterns above can be combined to create larger patterns. For example::
109100
110− f = \o -> case o of
111− { arr = x : _, take = "car" } -> x
112− { arr = _ : x : _, take = "cadr" } -> x
113− _ -> 0
101+ f { arr = x : _, take = "car" } = x
102+ f { arr = _ : x : _, take = "cadr" } = x
103+ f _ = 0
114104
115105Named Patterns
116106--------------
117107
118−Named patterns bring additional names into scope when using nested patterns. Any pattern can be named by using the `@` symbol::
108+Named patterns bring additional names into scope when using nested patterns. Any pattern can be named by using the ``@`` symbol::
119109
120− f = \arr -> case arr of
121− a@(_ : _ : _) -> true
122− a -> false
110+ f a@(_ : _ : _) = true
111+ f _ = false
123112
124−Here, in the first pattern, any array with two or more elements will be matched and bound to the variable `a`.
113+Here, in the first pattern, any array with two or more elements will be matched and bound to the variable ``a``.
125114
126115Guards
127116------
128117
129118Guards are used to impose additional constraints inside a pattern using boolean-valued expressions, and are introduced with a pipe after the pattern::
130119
131− evens = \arr -> case arr of
132− [] -> 0
133− x : xs | x % 2 == 0 -> 1 + evens xs
134− _ : xs -> evens xs
120+ evens [] = 0
121+ evens (x : xs) | x % 2 == 0 = 1 + evens xs
122+ evens (_ : xs) = evens xs
135123
136124When using patterns to define a function at the top level, guards appear after all patterns::
137125
docs/source/syntax.rst+16−56
@@ -15,7 +15,6 @@ But this is not::
1515
1616 foo = bar(x) +
1717 baz(x)
18− data UnaryOperator
1918
2019Operators
2120---------
@@ -61,7 +60,7 @@ and the following binary operators (in precedence order):
6160Literal Values
6261--------------
6362
64−Numeric literals can be integers or floating point numbers. Numbers in hexadecimal notation should be preceded by the characters `0x`::
63+Numeric literals can be integers or floating point numbers. Numbers in hexadecimal notation should be preceded by the characters ``0x``::
6564
6665 16
6766 16.0
@@ -123,53 +122,14 @@ which would correspond to the following Javascript::
123122 }
124123 }
125124
126−Multiple argument functions can be introduced by wrapping the arguments in parentheses, and separating them with commas::
125+Function application is indicated by just the juxtaposition of a function with its arugments::
127126
128− example2 = \(a, b) -> a + b
127+ example1 10 20
129128
130−which generates::
129+Functions can also be defined at the top level by providing a list of patterns and an optional guard on the left hand side of the equals sign::
131130
132− function example2(a, b) {
133− return a + b;
134− }
135−
136−There is a special case for functions with no arguments, where the parentheses may be omitted, as follows::
137−
138− example3 = \ -> 100
139−
140−which would correspond to the Javascript::
141−
142− function example3() {
143− return 100;
144− }
145−
146−Multiple-argument and single-argument syntax can be mixed, as follows::
147−
148− example4 = \a (b, c) d -> a + b + c + d
149−
150−which generates::
151−
152− function example4(a) {
153− return function (b, c) {
154− return function (d) {
155− return a + b + c + d;
156− }
157− }
158− }
159−
160−Functions are applied by providing their arguments inside parentheses::
161−
162− test1(1, 2, 3)
163− test2()
164−
165−A special case is made in the case of functions with one argument. These functions can be applied without parentheses, and function application associates to the left::
166−
167− addThree :: Number -> Number -> Number -> Number
168− addThree a b c = a + b + c
169−
170− addThree 1 :: Number -> Number -> Number
171− addThree 1 2 :: Number -> Number
172− addThree 1 2 3 :: Number
131+ f [] [] = []
132+ f (x:xs) (y:ys) = x + y : f xs ys
173133
174134If-Then-Else Expressions
175135------------------------
@@ -204,23 +164,23 @@ Here is an example, using the maybe monad::
204164 isEven sum
205165 ret sum
206166
207−`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`.
167+``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``.
208168
209−When using `do` notation, the corresponding type constructor must be an instance of the `Prelude.Monad` type class, which defines the `ret` and `>>=` functions.
169+When using `do` notation, the corresponding type constructor must be an instance of the `Prelude.Monad` type class, which defines the ``ret`` and ``>>=`` functions.
210170
211171Statements can have the following form:
212172
213−- `a <- x` which desugars to `m.bind x (\a -> ...)`
214−- `let a = x` which desugars to `(\a -> ...)(x)`
215−- `x` which desugars to `m.bind x (\_ -> ...)` or just `x` if this is the last statement.
173+- ``a <- x`` which desugars to ``m.bind x (\a -> ...)` `
174+- ``let a = x`` which desugars to ``(\a -> ...)(x)``
175+- ``x`` which desugars to ``m.bind x (\_ -> ...)`` or just ``x`` if this is the last statement.
216176
217−Binders can be used on the left hand side of `<-` or `=`. For example::
177+Binders can be used on the left hand side of ``<-`` or ``=``. For example::
218178
219179 test arr = do
220180 (x:y:_) <- arr
221181 ret (x + y)
222182
223−A pattern match failure will generate a runtime exception, just as in the case of a regular `case` statement.
183+A pattern match failure will generate a runtime exception, just as in the case of a regular ``case`` statement.
224184
225185Operators
226186---------
@@ -251,11 +211,11 @@ Properties on records can be updated using the following syntax::
251211
252212 o { key = value, ..., key = value }
253213
254−For example, the following function increments the `foo` property on its argument::
214+For example, the following function increments the ``foo`` property on its argument::
255215
256− incr = \o -> o { foo = o.foo + 1 }
216+ incr o = o { foo = o.foo + 1 }
257217
258−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::
218+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::
259219
260220 Object.prototype.extend = function(o, p) {
261221 return $.extend({}, o, p);
docs/source/types.rst+1−1
@@ -51,7 +51,7 @@ In the example, Foo is a tagged union type which has two constructors. It's firs
5151Functions
5252---------
5353
54−Functions in PureScript can have zero or more arguments. A function with multiple arguments is not to be confused with a `curried function`, which takes a single argument, and returns another function.
54+Functions in PureScript are like their Javascript counterparts, but always have exactly one argument.
5555
5656Polymorphic Types
5757-----------------