Like every language, Erlang has drawers full of parts that are fun to peruse. These are a very few of the more common ones. For much much more, see http://bitly.com/10CiiKR.
You can use most Erlang functions from the shell, but these are ones that are exclusive to the shell.
Command | Action |
---|---|
|
Quits the shell and the Erlang runtime |
|
Compiles the specified Erlang file |
|
Displays all variable bindings |
|
Clears all variable bindings |
|
Clears specified variable binding |
|
Prints the history list of commands |
|
Repeats the command on line |
|
The return value of line |
|
Sets how strict the shell will be in passing errors |
|
Defines a record type |
|
Defines record types based on the contents of |
|
Clears all record definitions. Can also clear specific definitions |
|
Lists all current record definitions |
|
Gets the present working directory |
|
Lists files at the current location |
|
Changes to the specified |
There are a few Erlang terms you can’t use outside of their intended context.
The Erlang compiler will wonder what you’re trying to do if you use certain keywords as atoms or function names. It will try to treat your atoms as if they were code, and you can get very strange errors. After all, you should be able to have something called band
, right? Unfortunately, no. band
is one of the reserved words.
after |
and |
andalso |
band |
begin |
bnot |
bor |
bsl |
bsr |
bxor |
case |
catch |
cond |
div |
end |
fun |
if |
let |
not |
of |
or |
orelse |
query |
receive |
rem |
try |
when |
xor |
For function names, the answer is simple: use something else. If you want to use these as atoms, however, you can. You just need to enclose the offending reserved word in single quotes: 'receive'
, for example.
While they aren’t reserved words, there are also a few atoms commonly used in return values. It’s probably best to use them only in circumstances where they’re normally expected.
Atom | Means |
---|---|
|
Normal exit to a method. (Does not mean that whatever you asked for succeeded.) |
|
Something went wrong. Typically accompanied by a larger explanation. |
|
A value hasn’t been assigned yet. Common in record instances. |
|
A reply is included with some kind of return value. |
|
No return value is included. A response of some sort may come, however, from other communication. |
|
Used in OTP to signal that a server should stop, and triggers the |
|
Returned by OTP supervisor process that can’t start a child. |
Operator | Description |
---|---|
|
logical and |
|
logical or |
|
logical xor |
|
unary logical not |
The not
operator is processed first.
andalso
and orelse
are also boolean operators for logical and and logical or, but they are short-circuit operators. If they don’t need to process all the possibilities in their arguments, they stop at the first one that gives them a definite answer.
Operator | Description |
---|---|
|
equal to |
|
not equal to |
|
less than or equal to |
|
less than |
|
greater than or equal to |
|
greater than |
|
exactly equal to |
|
exactly not equal to |
You can compare elements of different types in Erlang. The relationship of types from “least” to “greatest” is:
number < atom < reference < fun < port < pid < tuple < list < bit string
Within number
, you can compare integers and floats except with the more specific =:=
and =/=
operators, both of which will return false
when you compare numbers of different types.
You can also compare tuples even when they contain different numbers of values. Erlang will go through the tuples from left to right and evaluate on the first value that returns a clear answer.
Operator | Description |
---|---|
|
unary + (positive) |
|
unary - (negative) |
|
addition |
|
subtraction |
|
multiplication |
|
floating point division |
|
integer division |
|
integer remainder of X/Y |
Operator | Description |
---|---|
|
unary bitwise not |
|
bitwise and |
|
bitwise or |
|
arithmetic bitwise xor |
|
arithmetic bitshift left |
|
bitshift right |
Operator | Associativity |
---|---|
|
|
|
|
Unary |
|
|
Left associative |
|
Left associative |
|
Right associative + |
== /= =< < >= > =:= =/=+ |
|
|
|
|
|
|
Right associative |
|
The highest priority operator in an expression is evaluated first. Erlang evaluates operators with the same priority by following associative paths. (Left associative operators go left to right, right associative operators go right to left.)
Erlang allows only a limited subset of functions and other features in guard expressions, going well beyond a “no side effects” rule to keep a simple subset of possibilities. The list of allowed components includes the following:
true
Other constants (regarded as false
)
Term comparisons (Table A-5)
Boolean expressions and short-circuit expressions (andalso
and orelse
)
The following functions: abs/1
, bit_size/1
, byte_size/1
, element/2
, float/1
, hd/1
, is_atom/1
, is_binary/1
, is_bitstring/1
, is_boolean/1
, is_float/1
, is_function/1
, is_function/2
, is_integer/1
, is_list/1
, is_map/1
, is_number/1
, is_pid/1
, is_port/1
, is_record/2
, is_record/3
, is_reference/1
, is_tuple/1
, length/1
, map_size/1
, node/0
, round/1
, self/0
, size/1
, tl/1
, trunc/1
, tuple_size/1
Function | Use |
---|---|
|
The constant pi |
|
Sine |
|
Cosine |
|
Tangent |
|
Inverse sine (arcsine) |
|
Inverse cosine (arcosine) |
|
Inverse tangent (arctangent) |
|
Arctangent that understands quadrants |
|
Hyperbolic sine |
|
Hyperbolic cosine |
|
Hyperbolic tangent |
|
Hyperbolic arcsine |
|
Hyperbolic arccosine |
|
Hyperbolic arctangent |
|
Exponential function |
|
Natural logarithm (base e) |
|
Logarithm (base 10) |
|
First argument to the second argument power |
|
Square root |
|
Error function |
|
Complementary error function |
Arguments for all trigonometric functions are expressed in radians. To convert degrees to radians, divide by 180 and multiply by pi.
The erf/1
and erfc/1
functions may not be implemented in Windows. The Erlang documentation also warns more broadly that “Not all functions are implemented on all platforms,” but these come directly from the C language libraries.
function | Returns | Use |
---|---|---|
|
|
Side effects specified in function |
|
new list |
Apply function to list values |
|
subset |
Creating list where function returns |
|
boolean |
Returns |
|
boolean |
Returns |
|
subset |
Collects the head of the list until the function is |
|
subset |
Deletes the head of the list until the function is |
|
accumulator |
Passes function list value and accumulator, forward-through list |
|
accumulator |
Passes function list value and accumulator, backward-through list |
|
tuple of two lists |
Split list based on function |
Chapter 7 describes these in greater detail.
Sequence | Produces |
---|---|
|
Value, pretty-printed |
|
Value, no indentation |
|
Contents of a string |
|
ASCII character corresponding to a number |
|
Unicode character corresponding to a number |
|
Ignores that item |
|
Newline (doesn’t reference argument list) |
Sequence | Produces |
---|---|
|
double quote |
|
single quote |
|
backslash |
|
backspace |
|
delete |
|
escape |
|
form feed |
|
newline |
|
carriage return |
|
space |
|
tab |
|
vertical tab |
|
character with octal representation XYZ, YZ or Z |
|
character in hex |
|
characters in hex, where X… is one or more hexadecimal characters |
|
control-A to control-Z |
Function | Returns |
---|---|
|
Length of the string (traverses string, so slows with big ones) |
|
Length of the string (traverses string, so slows with big ones) |
|
A single string containing the two parts from the arguments |
|
A single string containing all the parts from the arguments |
|
A single string containing all the parts from the arguments |
|
The character at the specified position |
|
First character of the string |
|
The position where the specified character first appears |
|
The position of a substring in a string |
|
A segment from a string at a given position of a given length |
|
A segment from a string between two positions |
|
A list of pieces from a string broken at the specified separators |
|
A string made from the list of pieces with specified separators added |
|
The number of words in the string |
|
A string that repeats a given character a given number of times |
|
A string that repeats a given string a given number of times |
|
A string with leading and/or trailing whitespace (or specified characters) removed |
|
A string of a specified length, padded with spaces on the right if needed |
|
A string of a specified length, padded with spaces on the left if needed |
|
A string of a specified length, padded with spaces on the left and right if needed |
|
A string in backwards order |
|
The float contents of the string, plus leftovers, or an error tuple |
|
The integer contents of the string, plus leftovers, or an error tuple |
|
A version of the string with all uppercase (Latin-1) characters converted to lowercase |
|
A version of the string with all lowercase (Latin-1) characters converted to uppercase |
|
A string version of an integer, optionally in a specified base |
|
A string version of a float |
|
A string version of a fun |
|
An atom version of a string |
Note: I wrote a single wrapper module that assembles Erlang’s tools for working with strings into one place. For more, visit https://github.com/simonstl/erlang-simple-string.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
For more, see http://www.erlang.org/doc/reference_manual/typespec.html.