Files
rexlang/examples/basics/list_values.rex
T
Your NameandClaude Opus 5.5 7cf9727bcd Rename the language from Dino to Rex
Files use .rex, the command is rex, and the crate, runtime prefixes,
docs, editor files and examples all use the new name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 13:51:23 +01:00

69 lines
1.2 KiB
Rexx

// Lists are values: assigning or passing copies them.
struct Inventory {
items: []string
gold: int
func add(item: string) {
items.push(item)
}
func count(): int {
return items.len()
}
}
func add_one(xs: []int): []int {
xs.push(1)
return xs
}
func first(xs: []int): int {
return xs[0]
}
func main() {
var a = [1, 2, 3]
var b = a
b[0] = 99
b.push(4)
print(a, b)
// a function that changes its parameter works on its own copy
var c = add_one(a)
print(a, c)
print(first(a), first(c))
// structs holding lists copy deeply
var inv = Inventory{[], 10}
inv.add("sword")
var saved = inv
inv.add("shield")
saved.gold = 0
print(inv)
print(saved)
print(inv.count(), saved.count())
// nested lists
var grid = [[1, 2], [3, 4]]
var snapshot = grid
grid[0][1] = 20
grid[1].push(5)
print(grid)
print(snapshot)
// looping over a list while changing it walks the original
var xs = [1, 2, 3]
for x in xs {
xs.push(x * 10)
}
print(xs)
// the loop variable is a copy too
var ys = [1, 2, 3]
for y in ys {
y += 100
print(y)
}
print(ys)
}