Erlang basics · Lesson 09 (open contents)
Put code in a module
Save functions in an `.erl` file, compile them, and look ahead to BEAM.
Save as village.erl
After saving, open erl in the same folder and run c(village)..
%% Keep the file name and module name the same
-module(village).
%% Allow code outside the module to call greet/1
-export([greet/1]).
%% Name is a binary
greet(Name) ->
<<"hello, ", Name/binary>>.- When compilation succeeds,
c(village).returns{ok,village}. village:greet(<<"Maya">>).returns<<"hello, Maya">>.
Read from the first line down
-module(village).declares the module name. The file should bevillage.erl./1in-export([greet/1]).says thatgreettakes one argument.Name/binaryappends the incoming binary to the fixed text.- After compiling, call it with
module:function(argument):village:greet(<<"Maya">>).
Symbols are not secret signs
-module(village).Declare the module name as village. The file should be called village.erl.
-export([greet/1]).Make the one-argument greet function public.
c(village).Compile and load village.erl in the Erlang shell.
Match each name to its meaning
module
A group of functions in one .erl file. The module name usually matches the file name.
export
A list of functions that code outside the module may call. Each item uses name/arity.
process
A small, independent unit of work on the BEAM. Processes work together by sending messages.
Why these forms are useful
The shell is like scratch paper. Named functions that need to be saved and reused usually belong in a module file. The file name and module name should match.
-export([greet/1]). allows code outside the module to call greet/1. The /1 matters because functions with the same name but different arities are different functions.
Once the module runs, you are ready for BEAM's strengths: small processes, messages, supervision, and recovery from failures. Those belong to the next learning path.
Why this shape?
Why organize functions in modules before reaching for a class?
A module names related functions and creates a public boundary. Data remains an explicit argument instead of hiding behind this.
Add a process only when state needs an owner, a lifecycle, or failure isolation. A GenServer around a pure function merely adds a queue.
Coming from Java, Python, or JavaScript
Java
- Familiar starting point
- A class combines data, methods, and object identity.
- What BEAM changes
- A BEAM module is not an object, and functions have no implicit receiver.
- False friend
- Calling a module does not start a process.
Python
- Familiar starting point
- Modules and classes can both organize code.
- What BEAM changes
- Module/function/arity forms the stable entry point; state is a separate design decision.
- False friend
- An OTP Application is not an operating-system process.
JavaScript
- Familiar starting point
- ES modules export functions or classes.
- What BEAM changes
- BEAM calls compiled modules by module name and arity.
- False friend
- Mix and rebar3 manage projects; neither is BEAM or OTP.
Close the answer and try
Add a public function farewell/1 that returns <<"bye, ", Name/binary>>.
-module(village).
%% Export one more function in this list
-export([greet/1, ____]).
greet(Name) ->
<<"hello, ", Name/binary>>.
%% Complete the new function
____(Name) ->
<<"bye, ", Name/binary>>.Target result: After compiling again, village:farewell(<<"Maya">>). should return <<"bye, Maya">>.
Stuck? Read one hint
Put farewell/1 in the export list. Put only farewell in the function definition.
After you run it, see one answer
-module(village).
%% Both functions take one argument
-export([greet/1, farewell/1]).
greet(Name) ->
<<"hello, ", Name/binary>>.
%% The final function definition ends with a dot
farewell(Name) ->
<<"bye, ", Name/binary>>.Think about it: Why does the export list say `greet/1` instead of only `greet`?
Erlang identifies a function by both its name and its number of arguments. greet/1 and greet/2 are different functions.
Remember these three lines
- 1
Keep the module name and
.erlfile name the same. - 2
An export list identifies functions with
name/arity. - 3
After learning the syntax, continue to BEAM processes and messages.