Language Tour
Guided walkthrough of Stage-0 Vela syntax and behavior.
1. First program
print("Hello, Vela!")
From hello.vela, this prints:
Hello, Vela!
hello.vela then defines a function and loops, demonstrating function declarations and range:
fn square(x):
return x * x
for n in range(1, 6):
print(n, "squared is", square(n))
2. Expressions as values
if, for, function bodies, and even match are expression-oriented.
fn sign(n):
if n < 0:
"negative"
elif n == 0:
"zero"
else:
"positive"
print(sign(-1), sign(0), sign(2))
3. Immutable and mutable bindings
x = 1
# x = 2 -> runtime error (immutable binding)
mut running = 0
for n in range(1, 4):
running = running + n
print(running)
Output:
6
4. Records and field access
Records are immutable named-field values:
p = { x: 3, y: 4 }
print(p.x + p.y)
p.x and p.y are field reads.
5. Enums and pattern matching
type Shape:
Circle(r)
Rect(w, h)
Point
fn area(s):
match s:
Circle(r) => 3.14159 * r * r
Rect(w, h) => w * h
Point => 0
print(area(Circle(2.0)), area(Rect(3, 4)), area(Point))
12.56636 12 0
6. Lists and iteration
nums = [1, 2, 3, 4]
for n in nums:
print(n)
for x in expr works on lists, ranges, and string iteration via chars.
7. Higher-order calls and pipelines
Stage-0 ships with map, filter, and reduce as builtins:
print([1, 2, 3, 4] |> filter(n => n % 2 == 0) |> map(n => n * n) |> reduce(0, (a, b) => a + b))
Output:
56
8. String work in practice
s = "Hello"
print(chars(s))
print(upper(s), lower("LOUD"))
print(split("a,b,c", ","))
["H", "e", "l", "l", "o"]
HELLO loud
["a", "b", "c"]
9. F-strings and expression embedding
name = "Ada"
score = 97
print(f"{name} scored {score}")
10. Mutable map style and loops in one pass
counts = {}
for w in split("the cat sat on the cat", " "):
counts = set(counts, w, get_or(counts, w, 0) + 1)
print(counts)