Skip to content
Variables and Atoms
step 1/5

Reading — step 1 of 5

Learn

~1 min readSyntax Basics

Variables in Erlang start with an UPPERCASE letter and are bound exactly once — you cannot reassign them. Lowercase identifiers are atoms — symbolic constants:

Name = "Ada",        % Variable
Age = 36,            % Variable
status = active,     % SYNTAX ERROR — lowercase isn't a variable
Status = active,     % OK — Status (var) bound to atom 'active'

Atoms are like enum values: their identity matters, not their contents. Common atoms: ok, error, true, false, undefined, nil.

Pattern matching is everywhere= doesn't assign, it asserts equality:

{X, Y} = {1, 2},     % Binds X=1, Y=2
[H | T] = [1, 2, 3], % H=1, T=[2,3]

Reading stdin:

Line = io:get_line(""),
%% strips trailing newline first:
Trimmed = string:trim(Line, trailing, "\n"),
N = list_to_integer(Trimmed)

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…