title | description | created | updated |
---|---|---|---|
Erlang |
Erlang cheatsheet gives you a quick reference to code syntax with examples makes it handy while coding. |
2020-07-02 |
2020-07-02 |
-module(helloworld).
-export([start/0]).
start() ->
io:fwrite("Hello World!").
- module : It is used to add a namespace like in other programming language.
- export : Any function which is defined in the program can be used by using export function.
/0
means thatstart
function accepts 0 parameters. - start() : start() is a function
- io : io is an module which has io all the required Input and Output functions.
- fwrite : to output data to the console.
- % : Used to provide comments
Variables syntax is as follows
variable-name = value
Data type | Usage | Description |
---|---|---|
Numeric | start() -> io:fwrite("~w",[10+10]) . |
Erlang supports both integer and float values. |
Atom | start() -> io:fwrite(true) | Atoms should start with lower case leters and can contain lowercase and uppercase characters, numbers, _ and @ . You can also put atom in single quotes |
Boolean | start() -> io:fwrite(10 =< 8) | Output will be either true or false based on the values given |
Bit String | str = <<10,20>> | Strings are enclosed in << >> and are used to store untyped memory |
Lists | [1,2,3] |
Lists is a compound data type with Variable number of elements. |
Tuples | t = {apple, 100, {orange,50}} | Tuple is a compound data type with fixed number of elements. |
Map | map = #{name=>onecompiler,message=>Learning} | Map is a compound data type with a variable number of key-value pairs |
Operator type | Description |
---|---|
Arithmetic Operator | + , - , * , / , rem, div |
Relational Operator | < , > , <= , >=, /= , == |
Logical Operator | and, or, not, xor |
Bitwise Operator | band, bor, bnot, bxor |
if
condition ->
true-statement;
true ->
false-statement
end.
case value of
value1 -> statement1;
value2 -> statement2;
valuen -> statementn
end.
As Erlang is a functional programming language, there are no direct constructs for while, for and other loops. Recursion is the technique followed to implement loops in Erlang.
FunctionName(Pattern1… PatternN) ->
%code
FunctionName(Pattern1… PatternN)