git.iko.soy/rowtype-yoga/purescript-native
on befaa35 help sign in
177 lines 4.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)
18 data UnaryOperator
20Operators
21---------
23PureScript supports the following unary operators (in precedence order):
25= =======================
26 Meaning
27= =======================
28- Numeric negation
29! Boolean negation
30~ Binary negation
31+ Unary plus (numeric)
32= =======================
34and the following binary operators (in precedence order):
36=== =====================
37 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
55<< Shift Left
56>> Shift Right
57>>> 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 are surrounded by square brackets, as in Javascript::
81 []
82 [1, 2, 3]
84Object literals are surrounded by braces, as in Javascript::
86 {}
87 { foo: "Foo", bar: 1 }
89If-Then-Else Expressions
90------------------------
92The `if`, `then` and `else` keywords can be used to create conditional expressions. In this case, the `else` block is always required.
94For example::
96 conditional = if 2 > 1 then "ok" else "oops"
98Do Notation
99-----------
101The `do` keyword introduces simple syntactic sugar for monadic expressions.
103Here is an example, using the maybe monad::
105 data Maybe a = Nothing | Just a
107 instance Monad Maybe where
108 ret = Just
109 (>>=) Nothing _ = Nothing
110 (>>=) (Just a) f = f a
112 isEven n | n % 2 == 0 = Just {}
113 isEven _ = Nothing
115 evenSum a b = do
116 n <- a
117 m <- b
118 let sum = n + m
119 isEven sum
120 ret sum
122`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`.
124When using `do` notation, the corresponding type constructor must be an instance of the `Prelude.Monad` type class, which defines the `ret` and `>>=` functions.
126Statements can have the following form:
128- `a <- x` which desugars to `m.bind x (\a -> ...)`
129- `let a = x` which desugars to `(\a -> ...)(x)`
130- `x` which desugars to `m.bind x (\_ -> ...)` or just `x` if this is the last statement.
132Binders can be used on the left hand side of `<-` or `=`. For example::
134 test arr = do
135 (x:y:_) <- arr
136 ret (x + y)
138A pattern match failure will generate a runtime exception, just as in the case of a regular `case` statement.
140Operators
141---------
143In addition to the standard operators, user-defined infix operators can be created by enclosing names in parentheses.
145E.g. to create a synonym for string concatenation::
147 (<>) = \s1 s2 -> s1 ++ s2
149 greeting = "Hello" <> "World!"
151Regular functions can be used as operators by enclosing their names in backticks::
153 foo = \x y -> x * y + y
155 test = 10 `foo` 20
157Fixity declarations can associate a precedence level, which is a natural number, to a user-defined operator, and specify which way it associates::
159 infixl 5 <>
160 infixr 7 %%
162Record Updates
163--------------
165Properties on records can be updated using the following syntax::
167 o { key = value, ..., key = value }
169For example, the following function increments the `foo` property on its argument::
171 incr = \o -> o { foo = o.foo + 1 }
173The 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::
175Object.prototype.extend = function(o, p) {
176 return $.extend({}, o, p);
177};