Erlang (programming language)/Tutorials/Terms: Difference between revisions

From Citizendium
Jump to navigation Jump to search
imported>Eric Evers
imported>Tom Morris
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
{{subpages}}
The basic unit of expressing a value in Erlang is the '''term'''. Terms are comprised of one of Erlang's simple or complex types.
The basic unit of expressing a value in Erlang is the '''term'''. Terms are comprised of one of Erlang's simple or complex types.



Latest revision as of 06:07, 8 August 2009


The basic unit of expressing a value in Erlang is the term. Terms are comprised of one of Erlang's simple or complex types.

Integers

Integer constants are written as numbers, optionally prefixed with a base and the octothorpe (#):

 1> 2.
 2
 2> 16#ff.
 255
 3> 2#1011.
 11

Erlang uses arbitrary-precision integers, which support integers with any number of digits.

Erlang also provides another syntactic shortcut: you can write an integer as the dollar sign ($) and a character, and the value is the ASCII value of that character. This is of use when handling strings, as strings are typically represented in Erlang as lists of integers (see Strings, below).

Floats

Floats (floating-point numbers) are written as numbers with decimal places, optionally with an exponent separated from the number with the letter e.

 4> 1.2.
 1.20000
 5> 1.0.
 1.00000
 6> 2.0e-4.
 2.00000e-4

Atoms

Atoms are named constants. Atoms begin with a lower-case letter and can contain letters, digits and the underscore character (_); or they are quoted with single quotes (').

 1> ok.
 ok
 2> 'OKAY'.
 'OKAY'
 3> this_is_an_atom.
 this_is_an_atom

The special atoms true and false represent boolean values.

Atoms are often used as the keys in key-value pairs, indicators of success and failure (e.g. ok and error) and to identify parts of a complicated structure for Pattern Matching.

Tuples

Tuples are terms composed of multiple values, and are of fixed length. Tuples are surrounded by braces ({ and }) and the elements of the tuple (any Erlang term, including tuples) separated by commas.

 1> {ok, 9}.
 {ok,9}
 2> {true, {127, 0, 0, 1}}.
 {true,{127,0,0,1}}
 3> {box, {width, 10}, {height, 35}}.

The 3rd examples shows something called a tagged tuple. Where possible use tagged tuples as they make it clear what the tuple is suppose to represent. Later on we will look at records which is just a shorthand for tagged tuples.

Lists

Lists are terms composed of multiple values, of varying length. Lists are surrounded by brackets ([ and ]) and each member of the list (any Erlang term, including lists) separated with commas.

Lists can also be composed of a head and tail portion, separated by the vertical bar character (|): [Head|Tail]. The Tail can be any valid Erlang term, but is usually a list representing the members of the list after the head.

 1> [one, two, three].
 [one,two,three]
 2> [1, 2|[3, 4, 5]].
 [1,2,3,4,5]
 3> [{key1, value1}, {key2, value2}].
 [{key1,value1},{key2,value2}]

Lists in which the smallest possible tail is the empty list ([]) are known as well-formed lists.

Strings

Erlang has no separate string type. Strings are usually represented by lists of integers (and the string module of the standard library manipulates such lists). Each integer represents the ASCII (or other character set encoding) value of the character in the string. For convenience, a string of characters enclosed in double quotes (") is equivalent to a list of the numerical values of those characters.

 1> "one".
 "one"
 2> [$o, $n, $e].
 "one"
 3> $o.
 111
 4> $n.
 110
 5> $e.
 101
 6> [111, 110, 101].
 "one"

The Erlang shell "guesses" whether a given list is a printable string and prints it that way for convenience. Also, there are a number of modules that enable unicode.

Pids

A Pid or process id is a special type in Erlang. To see what one looks like you can get the pid of yourself() with self().

1> self().
  <0.29.0>

Refs

A ref (or reference) is a term which is unique, even across Erlang nodes. You can create a new ref by calling erlang:make_ref/0.

2> erlang:make_ref().
#Ref<0.0.0.41>

Other types

Erlang has other types, such as funs (closures), bit-strings, binaries (continuous blocks of arbitrary data) and ports (port identifiers), that will be covered in their appropriate sections.