List Basics
Creating lists, accessing elements, appending, and getting length.
Example 021 of the basic Soma programs: a small, commented program that teaches one thing. Every output below was printed by soma 3.2.4 when this page was built.
How it is built
The cell ListBasics has no face and no memory. Handlers: run (line 17).
The source
021_list_basics.cell
// ============================================================================
// 021 — List Basics
// ============================================================================
// Creating lists, accessing elements, appending, and getting length.
//
// Run: soma run 021_list_basics.cell run
// Expect:
// nums: [1, 2, 3]
// length: 3
// appended: [1, 2, 3, 4]
// new length: 4
// empty: []
// empty length: 0
cell ListBasics {
on run() {
// list() creates a new list with the given elements
let nums = list(1, 2, 3)
print("nums: {nums}")
// .length gives the number of elements
let count = nums.length
print("length: {count}")
// list(existing, item) appends an item (creates new list)
let bigger = list(nums, 4)
print("appended: {bigger}")
let new_count = bigger.length
print("new length: {new_count}")
// Empty list
let empty = list()
print("empty: {empty}")
let elen = empty.length
print("empty length: {elen}")
return "done"
}
}What the toolchain says
$ soma check 021_list_basics.cell
✓ All checks passed.$ soma run 021_list_basics.cell run
nums: [1, 2, 3]
length: 3
appended: [[1, 2, 3], 4]
new length: 2
empty: []
empty length: 0
done