← Về danh sách bài học
Bài 2/20
💧 Bài 2: Kiểu dữ liệu cơ bản
🎯 Sau bài học này, bạn sẽ:
- Hiểu các kiểu dữ liệu cơ bản trong Elixir
- Làm việc với Atoms, Tuples, Lists, Maps
- Hiểu cách Strings hoạt động
1. Atoms
Atoms là constants có tên chính là giá trị của nó.
:ok
:error
:hello_world
# Atoms thường dùng cho status
{:ok, result}
{:error, reason}
2. Tuples
# Tuple - ordered, fixed size
{:ok, "Hello", 42}
tuple = {1, 2, 3}
elem(tuple, 0) # 1
3. Lists
# List - linked list
[1, 2, 3, 4, 5]
["a", "b", "c"]
# Head and Tail
[head | tail] = [1, 2, 3]
# head = 1, tail = [2, 3]
4. Maps
# Maps - key-value pairs
user = %{name: "John", age: 30}
user.name # "John"
user[:age] # 30
# Update map (creates new map)
updated = %{user | age: 31}
📝 Tóm Tắt
- Atoms: constants, thường dùng cho status
- Tuples: ordered, fixed size, O(1) access
- Lists: linked list, O(n) access
- Maps: key-value, giống dictionary