git.iko.soy/rowtype-yoga/purescript-native
on 98eaa39 help sign in
266 lines 7.2 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---------
22Binary operators in PureScript are just regular functions. The Prelude defines a number of operators which correspond to Javascript's operators.
24The Prelude defines the following functions which correspond to Javascript's unary operators:
26========== ======== =======================
27Function Operator Meaning
28========== ======== =======================
29negate \- Numeric negation
30not ! Boolean negation
31complement ~ Binary negation
32========== ======== =======================
34and the following binary operators:
36========== =========== =====================
37Function JS Operator Meaning
38========== =========== =====================
39\+ \+ Numeric addition
40\- \- Numeric subtraction
41\* \* Numeric multiplication
42/ / Numeric division
43% % Numeric modulus
44== == Equality check
45/= != Inequality check
46< < Less than
47<= <= Less than or equal
48> > Greater than
49>= >= Greater than or equal
50&& && Boolean AND
51|| || Boolean OR
52& & Binary AND
53\| \| Binary OR
54^ ^ Binary XOR
55shl << Shift Left
56shr >> Shift Right
57zshr \>>> Zero-Fill Shift Right
58++ \+ String concatenation
59========== =========== =====================
61Literal Values
62--------------
64Numeric literals can be integers or floating point numbers. Numbers in hexadecimal notation should be preceded by the characters ``0x``::
66 16
67 16.0
68 0xF0
70String literals are enclosed in double-quotes and may extend over multiple lines. Line breaks should be surrounded by slashes as follows::
72 "Hello World"
74 "Hello \
75 \World"
77The two boolean literals are ``true`` and ``false``.
79Array Literals
80--------------
82Array literals are surrounded by square brackets, as in Javascript::
84 []
85 [1, 2, 3]
87Record Literals
88---------------
90Record literals are surrounded by braces, as in Javascript::
92 {}
93 { foo: "Foo", bar: 1 }
95Array Indexing
96--------------
98The ``Prelude.Unsafe`` module defines the ``unsafeIndex`` function which retrieves the element of an array at an index::
100 unsafeIndex :: forall a. [a] -> Number -> a
102The code generator will turn the expression ``unsafeIndex arr index`` into the simplified Javascript ``arr[index]``.
104The ``purescript-array`` core library defines an alternative safe version ``!!`` of ``unsafeIndex`` which checks arrays bounds and returns a value of type ``Maybe a``.
106Property Accessors
107------------------
109To access a property of a record, use a dot, followed by the property name, as in Javascript::
111 rec.propertyName
113Functions
114---------
116Functions are introduced by using a backslash followed by a list of argument names::
118 example1 = \a b -> a + b
120which would correspond to the following Javascript::
122 function example1(a) {
123 return function (b) {
124 return a + b;
125 }
126 }
128Function application is indicated by just the juxtaposition of a function with its arugments::
130 example1 10 20
132Functions 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::
134 f [] [] = []
135 f (x:xs) (y:ys) = x + y : f xs ys
137If-Then-Else Expressions
138------------------------
140The ``if``, ``then`` and ``else`` keywords can be used to create conditional expressions. In this case, the ``else`` block is always required.
142For example::
144 conditional = if 2 > 1 then "ok" else "oops"
146Do Notation
147-----------
149The ``do`` keyword introduces simple syntactic sugar for monadic expressions.
151Here is an example, using the maybe monad::
153 data Maybe a = Nothing | Just a
155 isEven :: Number -> Maybe Unit
156 isEven n | n % 2 == 0 = Just unit
157 isEven _ = Nothing
159 evenSum a b = do
160 n <- a
161 m <- b
162 let sum = n + m
163 isEven sum
164 return sum
166``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``.
168When using ``do`` notation, the corresponding type constructor must be an instance of the ``Prelude.Monad`` type class.
170Statements can have the following form:
172- ``a <- x`` which desugars to ``x >>= \a -> ...``
173- ``x`` which desugars to ``x >>= \_ -> ...`` or just ``x`` if this is the last statement.
174- A let binding ``let a = x`` where ``a`` can be either a name or a binder. Note the lack of the ``in`` keyword.
176The example ``evenSum`` desugars to::
178 evenSum a b =
179 a >>= \n ->
180 b >>= \m ->
181 let sum = n + m in
182 isEven sum >>= \_ ->
183 return sum
185Binders can be used on the left hand side of ``<-`` or ``=``. For example::
187 test arr = do
188 (x:y:_) <- arr
189 ret (x + y)
191A pattern match failure will generate a runtime exception, just as in the case of a regular ``case`` statement.
193Operators
194---------
196User-defined infix operators can be created by enclosing names in parentheses.
198E.g. to create an infix synonym for the ``Data.Array.range`` function::
200 (..) :: Number -> Number -> [Number]
201 (..) = Data.Array.range
203This function can be used as follows::
205 oneToTen = 1 .. 10
207Regular functions can be used as operators by enclosing their names in backticks::
209 foo x y = x * y + y
211 test = 10 `foo` 20
213Fixity declarations can associate a precedence level, which is a natural number, to a user-defined operator, and specify which way it associates::
215 infix 5 ..
216 infixl 7 %%
217 infixr 9 ^^
219Record Updates
220--------------
222Properties on records can be updated using the following syntax::
224 o { key = value, ..., key = value }
226For example, the following function increments the ``foo`` property on its argument::
228 incr o = o { foo = o.foo + 1 }
230Let Bindings
231------------
233The ``let`` keywords a collection of local declarations, which may be mutually recursive, and which may include type declarations::
235 factorial :: Number -> Number
236 factorial =
237 let
238 go :: Number -> Number -> Number
239 go acc 1 = acc
240 go acc n = go (acc * n) (n - 1)
241 in
242 go 1
244The ``where`` keyword can also be used to introduce local declarations `at the end of a value declaration`::
246 factorial :: Number -> Number
247 factorial = go 1
248 where
249 go :: Number -> Number -> Number
250 go acc 1 = acc
251 go acc n = go (acc * n) (n - 1)
253Comments
254--------
256A single line comment starts with ``--``::
258 -- This is a comment
260Multi-line comments are enclosed in ``{-`` and ``-}``. These can be nested::
262 {- Comment {- nested comment -} continued comment -}