Learning Erlang L1
Todays goal: Don't understand it. Don't try to perfect it. Just use it.
Todays goal: Don't understand it. Don't try to perfect it. Just use it.
Start your Erlang Container.
You should see something like
Erlang/OTP 28 [erts-16.4] [source] [64-bit] [smp:128:4] [ds:128:4:10] [...]
Eshell V16.4 (press Ctrl+G to abort, type help(). for help)
1>That's your playground π
Let's start experimenting.
2 + 3.π‘
Important: Period at the end
X = 5.
X.
X = 6.π
This HAS to fail.
This is your first encounter with immutability.
This is your first encounter with immutability.
First function:
Double = fun(X) -> X * 2 end.
Double(4).
π‘
This is an anonymous function.
Using a list:
[1,2,3].
hd([1,2,3]).
tl([1,2,3]).
Aha-moment?
[Head | Tail] = [1,2,3].
Head.
Tail.
π
This is pattern matching in action. And this is one of the most important concepts of all.
Some more fun with fun() π
Square = fun(X) -> X * X end.
Square(5).Try deliberately breaking something:
wrong type
wrong pattern
Triple = fun(X) -> X * 3 end.
Functions are simply values.
Use pattern matching
IsZero = fun
(0) -> true;
(_) -> false
end.
Test it:
IsZero(0)IsZero(5)
Try deliberately breaking something:
- IsZero("0")