7Syntax is whitespace sensitive. The general rule of thumb is that declarations which span multiple lines should be indented past the column on which they were first defined on their subsequent lines.
9That is, the following is valid::
23PureScript supports the following unary operators (in precedence order):
25== =======================
27== =======================
31\+ Unary plus (numeric)
32== =======================
34and the following binary operators (in precedence order):
36==== =====================
38==== =====================
41\* Numeric multiplication
49>= Greater than or equal
57\>>> Zero-Fill Shift Right
58++ String concatenation
59==== =====================
64Numeric literals can be integers or floating point numbers. Numbers in hexadecimal notation should be preceded by the characters `0x`::
70String literals are enclosed in double-quotes and may extend over multiple lines. Line breaks should be surrounded by slashes as follows::
77The two boolean literals are ``true`` and ``false``.
82Array literals are surrounded by square brackets, as in Javascript::
90Record literals are surrounded by braces, as in Javascript::
93 { foo: "Foo", bar: 1 }
98To retrieve the element of an array at an index, use the ``!!`` operator::
102This example corresponds to the Javascript ``arr[0]``.
107To access a property of a record, use a dot, followed by the property name, as in Javascript::
114Functions are introduced by using a backslash followed by a list of argument names::
116 example1 = \a b -> a + b
118which would correspond to the following Javascript::
120 function example1(a) {
121 return function (b) {
126Multiple argument functions can be introduced by wrapping the arguments in parentheses, and separating them with commas::
128 example2 = \(a, b) -> a + b
132 function example2(a, b) {
136There is a special case for functions with no arguments, where the parentheses may be omitted, as follows::
140which would correspond to the Javascript::
142 function example3() {
146Multiple-argument and single-argument syntax can be mixed, as follows::
148 example4 = \a (b, c) d -> a + b + c + d
152 function example4(a) {
153 return function (b, c) {
154 return function (d) {
155 return a + b + c + d;
160Functions are applied by providing their arguments inside parentheses::
165A 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::
167 addThree :: Number -> Number -> Number -> Number
168 addThree a b c = a + b + c
170 addThree 1 :: Number -> Number -> Number
171 addThree 1 2 :: Number -> Number
172 addThree 1 2 3 :: Number
174If-Then-Else Expressions
175------------------------
177The ``if``, ``then`` and ``else`` keywords can be used to create conditional expressions. In this case, the ``else`` block is always required.
181 conditional = if 2 > 1 then "ok" else "oops"
186The `do` keyword introduces simple syntactic sugar for monadic expressions.
188Here is an example, using the maybe monad::
190 data Maybe a = Nothing | Just a
192 instance Monad Maybe where
194 (>>=) Nothing _ = Nothing
195 (>>=) (Just a) f = f a
197 isEven n | n % 2 == 0 = Just {}
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`.
209When using `do` notation, the corresponding type constructor must be an instance of the `Prelude.Monad` type class, which defines the `ret` and `>>=` functions.
211Statements can have the following form:
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.
217Binders can be used on the left hand side of `<-` or `=`. For example::
223A pattern match failure will generate a runtime exception, just as in the case of a regular `case` statement.
228In addition to the standard operators, user-defined infix operators can be created by enclosing names in parentheses.
230E.g. to create a synonym for string concatenation::
232 (<>) = \s1 s2 -> s1 ++ s2
234 greeting = "Hello" <> "World!"
236Regular functions can be used as operators by enclosing their names in backticks::
238 foo = \x y -> x * y + y
242Fixity declarations can associate a precedence level, which is a natural number, to a user-defined operator, and specify which way it associates::
250Properties on records can be updated using the following syntax::
252 o { key = value, ..., key = value }
254For example, the following function increments the `foo` property on its argument::
256 incr = \o -> o { foo = o.foo + 1 }
258The 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::
260 Object.prototype.extend = function(o, p) {
261 return $.extend({}, o, p);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list