git.iko.soy/rowtype-yoga/purescript-native
on c0eebf5 help sign in
222 lines 5.5 kB history raw
1Syntax
2======
4Whitespace Rules
5----------------
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::
11 foo = bar(x) +
12 baz(x)
14But this is not::
16 foo = bar(x) +
17 baz(x)
19Operators
20---------
22PureScript supports the following unary operators (in precedence order):
24== =======================
25 Meaning
26== =======================
27\- Numeric negation
28! Boolean negation
29~ Binary negation
30\+ Unary plus (numeric)
31== =======================
33and the following binary operators (in precedence order):
35==== =====================
36 Meaning
37==== =====================
38\+ Numeric addition
39\- Numeric subtraction
40\* Numeric multiplication
41/ Numeric division
42% Numeric modulus
43== Equality check
44!= Inequality check
45< Less than
46<= Less than or equal
47> Greater than
48>= Greater than or equal
49&& Boolean AND
50|| Boolean OR
51& Binary AND
52\| Binary OR
53^ Binary XOR
54<< Shift Left
55>> Shift Right
56\>>> Zero-Fill Shift Right
57++ String concatenation
58==== =====================
60Literal Values
61--------------
63Numeric literals can be integers or floating point numbers. Numbers in hexadecimal notation should be preceded by the characters ``0x``::
65 16
66 16.0
67 0xF0
69String literals are enclosed in double-quotes and may extend over multiple lines. Line breaks should be surrounded by slashes as follows::
71 "Hello World"
73 "Hello \
74 \World"
76The two boolean literals are ``true`` and ``false``.
78Array Literals
79--------------
81Array literals are surrounded by square brackets, as in Javascript::
83 []
84 [1, 2, 3]
86Record Literals
87---------------
89Record literals are surrounded by braces, as in Javascript::
91 {}
92 { foo: "Foo", bar: 1 }
94Array Indexing
95--------------
97To retrieve the element of an array at an index, use the ``!!`` operator::
99 arr !! 0
101This example corresponds to the Javascript ``arr[0]``.
103Property Accessors
104------------------
106To access a property of a record, use a dot, followed by the property name, as in Javascript::
108 rec.propertyName
110Functions
111---------
113Functions are introduced by using a backslash followed by a list of argument names::
115 example1 = \a b -> a + b
117which would correspond to the following Javascript::
119 function example1(a) {
120 return function (b) {
121 return a + b;
122 }
123 }
125Function application is indicated by just the juxtaposition of a function with its arugments::
127 example1 10 20
129Functions 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::
131 f [] [] = []
132 f (x:xs) (y:ys) = x + y : f xs ys
134If-Then-Else Expressions
135------------------------
137The ``if``, ``then`` and ``else`` keywords can be used to create conditional expressions. In this case, the ``else`` block is always required.
139For example::
141 conditional = if 2 > 1 then "ok" else "oops"
143Do Notation
144-----------
146The `do` keyword introduces simple syntactic sugar for monadic expressions.
148Here is an example, using the maybe monad::
150 data Maybe a = Nothing | Just a
152 instance Monad Maybe where
153 ret = Just
154 (>>=) Nothing _ = Nothing
155 (>>=) (Just a) f = f a
157 isEven n | n % 2 == 0 = Just {}
158 isEven _ = Nothing
160 evenSum a b = do
161 n <- a
162 m <- b
163 let sum = n + m
164 isEven sum
165 ret sum
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``.
169When using `do` notation, the corresponding type constructor must be an instance of the `Prelude.Monad` type class, which defines the ``ret`` and ``>>=`` functions.
171Statements can have the following form:
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.
177Binders can be used on the left hand side of ``<-`` or ``=``. For example::
179 test arr = do
180 (x:y:_) <- arr
181 ret (x + y)
183A pattern match failure will generate a runtime exception, just as in the case of a regular ``case`` statement.
185Operators
186---------
188In addition to the standard operators, user-defined infix operators can be created by enclosing names in parentheses.
190E.g. to create a synonym for string concatenation::
192 (<>) = \s1 s2 -> s1 ++ s2
194 greeting = "Hello" <> "World!"
196Regular functions can be used as operators by enclosing their names in backticks::
198 foo = \x y -> x * y + y
200 test = 10 `foo` 20
202Fixity declarations can associate a precedence level, which is a natural number, to a user-defined operator, and specify which way it associates::
204 infixl 5 <>
205 infixr 7 %%
207Record Updates
208--------------
210Properties on records can be updated using the following syntax::
212 o { key = value, ..., key = value }
214For example, the following function increments the ``foo`` property on its argument::
216 incr o = o { foo = o.foo + 1 }
218The 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::
220 Object.prototype.extend = function(o, p) {
221 return $.extend({}, o, p);
222 };