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::
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
31complement ~ Binary negation
32========== ======== =======================
34and the following binary operators:
36========== =========== =====================
37Function JS Operator Meaning
38========== =========== =====================
39(\+) \+ Numeric addition
40(\-) \- Numeric subtraction
41(\*) \* Numeric multiplication
45(/=) != Inequality check
47(<=) <= Less than or equal
49(>=) >= Greater than or equal
57zshr \>>> 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) {
126Function application is indicated by just the juxtaposition of a function with its arugments::
130Functions 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::
133 f (x:xs) (y:ys) = x + y : f xs ys
135If-Then-Else Expressions
136------------------------
138The ``if``, ``then`` and ``else`` keywords can be used to create conditional expressions. In this case, the ``else`` block is always required.
142 conditional = if 2 > 1 then "ok" else "oops"
147The ``do`` keyword introduces simple syntactic sugar for monadic expressions.
149Here is an example, using the maybe monad::
151 data Maybe a = Nothing | Just a
153 instance Monad Maybe where
155 (>>=) Nothing _ = Nothing
156 (>>=) (Just a) f = f a
158 isEven n | n % 2 == 0 = Just {}
168``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``.
170When using ``do`` notation, the corresponding type constructor must be an instance of the ``Prelude.Monad`` type class, which defines the ``ret`` and ``>>=`` functions.
172Statements can have the following form:
174- ``a <- x`` which desugars to ``m.bind x (\a -> ...)` `
175- ``let a = x`` which desugars to ``(\a -> ...)(x)``
176- ``x`` which desugars to ``m.bind x (\_ -> ...)`` or just ``x`` if this is the last statement.
178Binders can be used on the left hand side of ``<-`` or ``=``. For example::
184A pattern match failure will generate a runtime exception, just as in the case of a regular ``case`` statement.
189User-defined infix operators can be created by enclosing names in parentheses.
191E.g. to create a synonym for string concatenation::
193 (<>) = \s1 s2 -> s1 ++ s2
195 greeting = "Hello" <> "World!"
197Regular functions can be used as operators by enclosing their names in backticks::
199 foo = \x y -> x * y + y
203Fixity declarations can associate a precedence level, which is a natural number, to a user-defined operator, and specify which way it associates::
211Properties on records can be updated using the following syntax::
213 o { key = value, ..., key = value }
215For example, the following function increments the ``foo`` property on its argument::
217 incr o = o { foo = o.foo + 1 }
222The ``let`` keywords a collection of local declarations, which may be mutually recursive, and which may include type declarations::
224 factorial :: Number -> Number
227 go :: Number -> Number -> Number
229 go acc n = go (acc * n) (n - 1)
233The ``where`` keyword can also be used to introduce local declarations `at the end of a value declaration`::
235 factorial :: Number -> Number
238 go :: Number -> Number -> Number
240 go acc n = go (acc * n) (n - 1)
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