Erlang Notes – Simple data types

Here is the first of some notes about erlang.

Obviously this is a very cursory glance at things. It’s really just notes to help me remember things. If you’re looking for something in more detail you can take a look at the erlang reference manual.

Basic Data Types

Integers

  • Integer arithmetic is arbitrary sized. Yay for no overflows
  • Base 16 and 32 can be represented by 16#1a and 32#1a respectively. In fact, that
    works for arbitrary bases up to 36, I think. Base 2 is 2#1101

Floating-Point Numbers

  • Floating point numbers must have a decimal point followed by at least one decimal digit
  • As far as I can tell floats are also aribrarily sized.
  • Not sure yet how to specify precision, or if you can.
  • Cooercion from integer to float seems to be automatic, if one of the terms is a float

Atoms

  • Atoms usually start with lowercase letters. They can start with uppercase letters or non-alphanumerics if they are enclosed in
    quotes though.
  • foo, ‘Foo’, ‘@Foo’, dave@smith are all valid atoms
  • Atoms are used to represent symbolic constants, similar to Symbols in Ruby, or the #define FOO 1 construct in C

Tuples

  • A group of things enclosed in curly braces, {Foo, bar, 1, “22″} is a tuple
  • Tuples group a fixed number of things together. It performs the same function as a Struct in C or some other type of Record type of data structure
  • Tuples seem to be commonly used as a way of differentiating between functions that need to have different behaviours, but keep their arity the same. One example from Joe Armstrong’s book:


    area({rectangle, W, H}) -> W*H;
    area({circle, R}) -> 3.14 * R * R.

    By passing a tuple we are able to keep the arity the same, but cheat on the type of behaviour the function is implementing

Lists

  • A group of things enclosed in square brackets, [foo, X, 23, "toad"] is a list
  • Lists are useful for storing variable length numbers of things.
  • The first element of a list is called the head and the remainder is called the tail We can talk about these two elements using the general form [Head|Tail]. This is a common operation, especially when using recursive implementations of algorithms.

Strings

  • Strings are enclosed in double quotation marks. “My big ass” is a string.
  • Remember that things enclosed in single quotation marks are atoms – don’t confuse the two
  • Strings in erlang are really just lists of integers.
  • The shell will print a list of integers as a string if all the integers in the list represent printable characters under iso 8809 encoding

Leave a Comment