100% found this document useful (1 vote)
683 views1 page

Erlang CheatSheet (1 0)

This document provides a summary of key concepts in Erlang including: 1) Variables, data types (atoms, strings, lists, tuples), basic operations (pattern matching, accessing tuple elements), and I/O formatting. 2) Common functions like maps, comprehensions, and recursion for iterating over lists. 3) Records for defining and working with structured data. 4) Macros, modules, functions, and attributes for building reusable code through functions, exports, imports and includes. 5) Pattern matching and guards for conditional logic and control flow.

Uploaded by

nleghari
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
683 views1 page

Erlang CheatSheet (1 0)

This document provides a summary of key concepts in Erlang including: 1) Variables, data types (atoms, strings, lists, tuples), basic operations (pattern matching, accessing tuple elements), and I/O formatting. 2) Common functions like maps, comprehensions, and recursion for iterating over lists. 3) Records for defining and working with structured data. 4) Macros, modules, functions, and attributes for building reusable code through functions, exports, imports and includes. 5) Pattern matching and guards for conditional logic and control flow.

Uploaded by

nleghari
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Erlang CheatSheet v1.

Variable % comments (starts with upper case) atom or ‘ATOM’


Str = “John Doe”. % strings are stored as Macros:
integers. -define(macro1, Replacement)
?macro1 % to use the macro
% Writing the value on output Erlang Shell:
Io:format(“Name is: ~s~n”, [Str]). c(ModuleName) % compile module on erlang
Io:fwrite(“Name is: ~s~n”, [Str]). shell
~n => new line cd(“dirname”) % change directory on shell
~s => string f() to clear all existing bindings
~f => float rr("records.hrl") to read a records file
~w=>standard output. Like Object.toString(). rf(record_name) to forget a record
~p=>like ~w but breaks after each line

List = [1,2,3,4]. % lists Records:


NewList = [6, 7, List] returns [6,7, [1,2,3,4]] % -record(todo, {status=reminder,who=john,text}).
appends to a new list - to create an instance of record: X =
29> [H|T] = AList. #todo{status=urgent}.
["a","b",{1,2,3}] % returns the Head and Tail. H - extracting values from record is similar to
and T are Unbound variables. pattern matching
- X#todo.status %% to get a single value.
Tuple = {1.0, 2.0, 3.0} - ++ is the infix append operator
element(2, Tuple) returns 2.0 % tuple index - [1] ++ [2] ++ [3] = [1,2,3]
{_, Second, _} = Tuple stored 2.0 in Second - X--Y is the list subtraction operator. It subtracts
variable % pattern matching to retrieve a value the elements of Y from X.
List and Tuple can contain any type. Use pattern matching/recursion to replace
Atuple = {1,2,3}. iteration.
{1,2,3} total([{What,N|T}]) -> cost(What) * N + total(T);
AList = ["a", "b", Atuple]. total([]) -> 0.
["a","b",{1,2,3}]
Anewtuple = {atom1, atom2, AList}.
{atom1,atom2,["a","b",{1,2,3}]
Functions: File attributes % -import, -export, -module
Anonymous: -module(ModuleName)
F = fun(X) -> X end. % F(10) prints 10 -export([Func_a/0, Func_b/1]).
Named: - import to import the module and methods % -
method_name(Arg) -> import(lists, [map/2]).
Arg. - Includes File: -include(Filename). % -
include_lib(Name).
% Perform action on each element on list case Expression of
L = [1,2,3,4,5]. Pattern1 [when Guard1] -> Expr_seq1;
[1,2,3,4,5] Pattern2 [when Guard2] -> Expr_seq2
lists:map(fun(X) -> 2*X end, L). % using map end.
method of lists module
[2,4,6,8,10]
45> [2*X || X <- L]. % or using list
comprehensions
[2,4,6,8,10]

Nauman Leghari http://weblogs.asp.net/nleghari

You might also like