Skip to content

Instantly share code, notes, and snippets.

@jzelinskie
Last active December 14, 2015 05:59
Show Gist options
  • Save jzelinskie/5fd58892c9a9c1b9fb92 to your computer and use it in GitHub Desktop.
Save jzelinskie/5fd58892c9a9c1b9fb92 to your computer and use it in GitHub Desktop.
Erlang Notes

a list of fundamentals of Erlang extracted from LYSE.

shell

commands

  • help().
  • ctrl+g --> h

compilation

common flags

  • -debug_info
  • -{outdir,Dir}
  • -export_all
  • -{d,Macro}
  • -{d,Macro,Value}

native compilation

hipe:c(Module, OptionsList).

numbers

keywords

  • div (used for int->int)
  • rem (used for int->int)

examples

1> 2 + 15.
17
2> 49 * 100.
4900
3> 1892 - 1472.
420
4> 5 / 2.
2.5
5> 5 div 2.
2
6> 5 rem 2.
1
1> 2#101010. % binary
42
2> 8#0677.   % octal
447
3> 16#AE.    % hex
174

atoms

examples

1> atom = 'atom'.
atom

bools & comparisons

hierarchy

number < atom < reference < fun < port < pid < tuple < list < bitstring

keywords

  • and
  • or
  • xor
  • andalso ("short circuit" -- only evaluate to the right if neccessary)
  • orelse (short circuit)

examples

1> 5 =:= 5.  % exact quality
true
2> 5 == 5.0. % inexact quality
true
3> 5 =/= 5.  % exact inequality
false
4> 5 /= 5.0. % inexact inequality
false
5> 5 =:= true. % compare any types
false
% surpisingly, nothing weird about these comparison operators
1> 1 < 2.
true
2> 1 >= 1.
true

tuples

examples

1> Point = {4,5}.
{4,5}
2> {X,Y} = Point.
{4,5}
3> X.
4
4> {point, {X,Y}}. % tagged tuple
{point, {4,5}}

lists

builtins

  • hd(List).
  • tl(List).
  • length(List).

operators

  • ++ (concatenation)
  • -- (subtraction)
  • | (constructor)

examples

1> [97,98,99,100,233]. % lists are also unicode strings
"abcdé"
2> [1,2,3] ++ [4,5].
[1,2,3,4,5]
3> [1,2,3] -- [1,2] -- [3]. % ++ and -- are right associative
[3]
1> [Head|Tail] = List = [1,2,3].
[1,2,3]
2> Head = hd(List).
1
3> Tail = tl(List).
[2,3]
4> [1|2]. % improper lists won't work in list functions
[1|2]
5> [1|[2]] = [1|[2|[]]]. % proper list
[1,2]
% list comprehensions
1> [2 * N || N <- [1,2,3,4]].
[2,4,6,8]
2> [X+Y || X <- [1,2], Y <- [2,3]]. % [1+2,1+3,2+2,2+3]
[3,4,4,5]
3> [X || X <- [1,2,3,4,5,6,7,8,9,10], X rem 2 =:= 0].
[2,4,6,8,10]

bits

keywords

  • bsl (shift left)
  • bsr (shift right)
  • band
  • bor
  • bxor
  • bnot

syntax

<<Value:Size/TypeSpecifierListSeparatedByDashes>>.

type specifiers

  • integer
  • float
  • binary
  • bytes
  • bitstring
  • bits
  • utf8
  • utf16
  • utf32

signedness

  • signed
  • unsigned

endianness

  • big
  • little
  • native

bit units

  • unit

examples

1> <<Pix1:24,Pix2:24,Pix3:24,Pix4:24>> = <<213,45,132,64,76,32,76,0,0,234,32,15>>.
<<213,45,132,64,76,32,76,0,0,234,32,15>>
2> <<R:8, G:8, B:8>> = <<Pix1:24>>.
<<213,45,132>>
3> <<Y:4/little-unit:8>> = <<72,0,0,0>>.
<<72,0,0,0>>
4> Y.
72
5> <<"This is a binary string">>. % avoids the linked list aspect of normal strings
<<"This is a binary string">>
% binary comprehensions
1> Pixels = <<213,45,132,64,76,32,76,0,0,234,32,15>>.
<<213,45,132,64,76,32,76,0,0,234,32,15>>
2> RGB = [{R,G,B} || <<R:8,G:8,B:8>> <= Pixels].
[{213,45,132},{64,76,32},{76,0,0},{234,32,15}]

modules

builtins

module:module_info().

syntax

-module(ModuleNameAtom).
-export([ExportedFunction/ExportedFunctionsArity]).
-author("Jimmy Zelinskie").
-vsn(VersionNumber).

examples

1> erlang:element(2, {a,b,c}) =:= element(2, {a,b,c}.
true

functions

syntax

same(X,X) ->
  true;
same(_,_) ->
  false.
% in guards , = andalso, ; = orelse except they ignore exceptions
wrong_age(X) when X < 16, X > 21; X > 90 ->
  true;
wrong_age(_) ->
  false;

macros

syntax

-define(MACRO, some_value).

examples

-define(sub(X,Y), X-Y).
subtract(X,Y) ->
  ?sub(X,Y).

if ... "else"

syntax

if BooleanExpression -> Expression;
  true -> CatchAllExpression.

examples

if X > Y -> a()
 ; X =< Y -> b() % instead of using true, cover all cases
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment