3. Your First Alef Program
In this chapter, you will build a tiny ticket priority program. It is small enough to understand, but it uses the pieces you need in real Alef code: values, functions, maps, if, and output.
Step 1: Print Text
Create main.alef:
fn main() {
println("TicketDesk")
}
Run
alef run main.alef
Output
TicketDesk
Step 2: Store A Value
Change the file:
fn main() {
let title = "Checkout timeout"
println(title)
}
let creates a local value. title is the name. "Checkout timeout" is a string.
Output
Checkout timeout
Step 3: Use A Function
Functions make behavior reusable.
fn label(id, title) {
return "{id}: {title}"
}
fn main() {
println(label("TD-101", "Checkout timeout"))
}
Output
TD-101: Checkout timeout
Step 4: Make A Decision
Use if when a program needs to choose.
fn priority(plan, blocked) {
if plan == "enterprise" and blocked {
return "P0"
}
return "P2"
}
fn main() {
println(priority("enterprise", true))
}
Output
P0
Step 5: Put Data In A Map
Maps are good for JSON-like data.
fn priority(plan, blocked) {
if plan == "enterprise" and blocked {
return "P0"
}
return "P2"
}
fn main() {
let ticket = {
"id" => "TD-101",
"title" => "Checkout timeout",
"plan" => "enterprise",
"blocked" => true
}
let p = priority(ticket["plan"], ticket["blocked"])
let id = ticket["id"]
println("{id}: {p}")
}
Output
TD-101: P0
You now know enough to read the next chapters:
fndeclares a function.letbinds a value.- strings use double quotes.
- maps use
"key" => value. - map reads use
ticket["id"]. ifbranches on a condition.andcombines boolean conditions.returnexits a function with a value.