Skip to content

Instantly share code, notes, and snippets.

@mkuklis
Created November 5, 2012 04:19
Show Gist options
  • Save mkuklis/4015291 to your computer and use it in GitHub Desktop.
Save mkuklis/4015291 to your computer and use it in GitHub Desktop.
playing with erlang
% variables
One = 1.
Two = One + 1.
% atoms
atom.
'Hi I am an atom'.
another@_atom1.
% boolean algebra
true and false or true xor true.
% equality
5 =:= 5. % true
1 =:= 0. % false
1 =/= 0. % false
5 == 5.0. % true
5 =:= 5.0. % false
5 >= 0.
5 =< 6. % wtf erlang
0 == false. % more wtf
1 < false. % more wtf
% tuples
Point = {1,2}.
{X,Y} = Point.
X. % 1
{point, Point}. % {point, {1,2}}
% lists
[1, 2, 3, {numbers,[4,5,6]}, 5.34, atom].
[97,98,99,4,5,6].
[97,98,99]. % 'abc' wtf!!!
[1,2,3] ++ [4,5]. % 1,2,3,4,5
[1,2,3,4,5] -- [4,5]. % 1,2,3
hd([1,2,3]). % 1
tl([1,2,3]). % [2,3]
List = [2,3,4].
NewList = [1|List]. % [1,2,3,4]
% all the same
[1, 2, 3, 4].
[1, 2, 3, 4 | []].
[1, 2 | [3, 4]].
[1, 2 | [3 | [4]]].
[1 | [2 | [3 | [4]]]].
[1 | [2 | [3 | [4 | [] ]]]].
% List Comprehensions
[2*N || N <- [1,2,3,4]]. % [2,4,6,8].
[X || X <- [1,2,3,4,5,6,7,8,9,10], X rem 2 =:= 0]. % [2,4,6,8,10]
% NewList = [Expression || GeneratorExp1, GeneratorExp2, ..., GeneratorExpN, Condition1, Condition2, ... ConditionM]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment