Values in Erlang

Erlang has various value types including strings, integers, floats, atoms (similar to booleans), etc. Here are a few basic examples.

-module(values).
-export([main/0]).

main() ->
    % Strings, which can be concatenated with ++
    io:format("~s~n", ["erlang" ++ "lang"]),

    % Integers and floats
    io:format("1+1 = ~p~n", [1 + 1]),
    io:format("7.0/3.0 = ~p~n", [7.0 / 3.0]),

    % Atoms (similar to booleans), with logical operators
    io:format("~p~n", [true and false]),
    io:format("~p~n", [true or false]),
    io:format("~p~n", [not true]).

To run this Erlang program:

$ erlc values.erl
$ erl -noshell -s values main -s init stop
erlanglang
1+1 = 2
7.0/3.0 = 2.3333333333333335
false
true
false

In this Erlang code:

  1. We define a module named values and export the main/0 function.
  2. Strings in Erlang are actually lists of characters. We use ++ for concatenation.
  3. For printing, we use io:format/2. The first argument is a format string, and ~s is for strings, ~p for any term, and ~n for newline.
  4. Erlang uses and, or, and not for boolean operations instead of symbols.
  5. Erlang doesn’t have a dedicated boolean type. Instead, it uses the atoms true and false.

Erlang’s syntax and conventions are quite different from many other languages, reflecting its functional and concurrent nature. This example demonstrates some of the basic value types and operations in Erlang.