a list of fundamentals of Erlang extracted from LYSE.
help().
- ctrl+g --> h
- -debug_info
- -{outdir,Dir}
- -export_all
- -{d,Macro}
- -{d,Macro,Value}
hipe:c(Module, OptionsList).
- div (used for int->int)
- rem (used for int->int)
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
1> atom = 'atom'.
atom
number < atom < reference < fun < port < pid < tuple < list < bitstring
- and
- or
- xor
- andalso ("short circuit" -- only evaluate to the right if neccessary)
- orelse (short circuit)
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
1> Point = {4,5}.
{4,5}
2> {X,Y} = Point.
{4,5}
3> X.
4
4> {point, {X,Y}}. % tagged tuple
{point, {4,5}}
- hd(List).
- tl(List).
- length(List).
- ++ (concatenation)
- -- (subtraction)
- | (constructor)
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]
- bsl (shift left)
- bsr (shift right)
- band
- bor
- bxor
- bnot
<<Value:Size/TypeSpecifierListSeparatedByDashes>>.
- integer
- float
- binary
- bytes
- bitstring
- bits
- utf8
- utf16
- utf32
- signed
- unsigned
- big
- little
- native
- unit
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}]
module:module_info().
-module(ModuleNameAtom).
-export([ExportedFunction/ExportedFunctionsArity]).
-author("Jimmy Zelinskie").
-vsn(VersionNumber).
1> erlang:element(2, {a,b,c}) =:= element(2, {a,b,c}.
true
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;
-define(MACRO, some_value).
-define(sub(X,Y), X-Y).
subtract(X,Y) ->
?sub(X,Y).
if BooleanExpression -> Expression;
true -> CatchAllExpression.
if X > Y -> a()
; X =< Y -> b() % instead of using true, cover all cases
end