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::
19If-Then-Else Expressions
20------------------------
22The `if`, `then` and `else` keywords can be used to create conditional expressions. In this case, the `else` block is always required.
26 conditional = if 2 > 1 then "ok" else "oops"
31The `do` keyword introduces simple syntactic sugar for monadic expressions.
33Here is an example, using the maybe monad::
35 data Maybe a = Nothing | Just a
37 instance Monad Maybe where
39 (>>=) Nothing _ = Nothing
40 (>>=) (Just a) f = f a
42 isEven n | n % 2 == 0 = Just {}
52`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`.
54When using `do` notation, the corresponding type constructor must be an instance of the `Prelude.Monad` type class, which defines the `ret` and `>>=` functions.
56Statements can have the following form:
58- `a <- x` which desugars to `m.bind x (\a -> ...)`
59- `let a = x` which desugars to `(\a -> ...)(x)`
60- `x` which desugars to `m.bind x (\_ -> ...)` or just `x` if this is the last statement.
62Binders can be used on the left hand side of `<-` or `=`. For example::
68A pattern match failure will generate a runtime exception, just as in the case of a regular `case` statement.
73In addition to the standard operators, user-defined infix operators can be created by enclosing names in parentheses.
75E.g. to create a synonym for string concatenation::
77 (<>) = \s1 s2 -> s1 ++ s2
79 greeting = "Hello" <> "World!"
81Regular functions can be used as operators by enclosing their names in backticks::
83 foo = \x y -> x * y + y
87Fixity declarations can associate a precedence level, which is a natural number, to a user-defined operator, and specify which way it associates::
95Properties on records can be updated using the following syntax::
97 o { key = value, ..., key = value }
99For example, the following function increments the `foo` property on its argument::
101 incr = \o -> o { foo = o.foo + 1 }
103The 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::
105Object.prototype.extend = function(o, p) {
106 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