summaryrefslogtreecommitdiff
path: root/src/code/1/patterns
blob: b93fdc43ac523a4c0bd764cf453fef8f6ea560f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
iex> x = 1
1
iex> 1 = x
1
iex> x = 2
2
iex> 1 = x
** (MatchError) no match of right hand side value: 2

iex> [a, b, c] = [1, 2, 3]
[1, 2, 3]
iex> a
1
iex> b
2
iex> c
3
iex> [1, _, c] = [1, 2, 3]
[1, 2, 3]
iex> [2, _, d] = [1, 2, 3]
** (MatchError) no match of right hand side value: [1, 2, 3]

iex> [h|t] = [1, 2, 3]
[1, 2, 3]
iex> h
1
iex> t
[2, 3]

iex> %{a: 1} = %{a: 1, b: 2, c: 3}
%{a: 1, b: 2, c: 3}
iex> %{} = %{a: 3}
%{a: 3}

defmodule Foobar do
  def sum_list([]), do: 0
  def sum_list([h|t]), do: h + sum_list(t)
end

iex> Foobar.sum_list [1, 2, 3, 4, 5]
15

iex> << sign :: size(1), exp :: size(11), mantissa :: size(52)>> =  <<3.14159 :: float>>
<<64, 9, 249, 240, 27, 134, 110>>
iex> sign
0
iex> exp
1024
iex> mantissa
2570632149304942
iex> :math.pow(-1, sign) * (1 + mantissa / :math.pow(2, 52)) * :math.pow(2, exp - 1023)
3.14159