Lists
ASPL 0.7.0 · Current release
Append and sum typed values while a copied list keeps its old contents.
On this page
What you will learn
Build a typed list, append through a mutable root, sum its elements within bounds, and explain why a copy keeps its old contents.
Before you begin
Read loops for the counter and accumulator and strings for length and explicit integer formatting. This independent program supplies its own entry. Open a new Home draft, then choose Run explicitly. Earlier drafts remain available.
Append and sum
list<i64> / قائمة<ص64> names a list whose elements all have type i64 / ص64. Square brackets construct [2, 4, 6]. var / متغير makes numbers a mutable root. let copied = numbers / دع منسوخ = أعداد copies the current value before the append.
Execution: Browser and native · ASPL 0.7.0
- Read the example explanation in its lesson before running.
Input: No input lines are required.
fn start_main() -> i64 {
var numbers: list<i64> = [2, 4, 6];
let copied: list<i64> = numbers;
append(numbers, 8);
var index: i64 = 0;
var total: i64 = 0;
while (index < length(numbers)) {
total = total + numbers[index];
index = index + 1;
}
print(format_i64(total) + "\n");
print(format_i64(length(copied)) + "\n");
print(format_i64(copied[2]) + "\n");
return 0;
}
Expected outcomes
{
"browser": {
"check": "success",
"class": "success",
"output": "20\n3\n6\n",
"processStatus": 0
},
"native": {
"check": 0,
"status": 0,
"output": "20\n3\n6\n"
}
}append(numbers, 8) / أضف(أعداد, 8) adds one integer at the end. Append changes a mutable location and returns unit / وحدة, with no value to print. A let root cannot be appended to. Indexed assignment replaces an existing element; it does not grow the list.
length(numbers) / طول(أعداد) returns the element count. The while / طالما loop begins at index 0, reads numbers[index], adds it to total, and increments the index by 1. The strict comparison < length(numbers) prevents reading past the end. Each print / اطبع formats its integer with format_i64 / نسق_ص64 and adds an explicit \n. The entry returns normal status 0 after printing.
Predict the sum and the copy
After appending, numbers has four elements at indexes 0, 1, 2, 3. The sum is 2 + 4 + 6 + 8 = 20. Predict the copy's length and element at index 2 before Run. Exact output is 20\n3\n6\n, with status 0. copied still contains [2, 4, 6]: assignment copies a value, and later mutation does not create a shared mutable alias. This also applies to nested list values.
Valid reads satisfy 0 <= index < length(list). This separately labelled intentional failure reads index 3 from the original three-element list. It checks successfully, then traps with list-index-out-of-bounds, prints nothing and has no normal entry status. Negative indexes also trap. A trap ends the run; it is not a recoverable result.
Execution: Browser and native · ASPL 0.7.0
- Read the example explanation in its lesson before running.
Input: No input lines are required.
fn start_main() -> i64 {
let numbers: list<i64> = [2, 4, 6];
print(format_i64(numbers[3]));
return 0;
}
Expected outcomes
{
"browser": {
"check": "success",
"class": "trap",
"output": "",
"reason": "list-index-out-of-bounds"
},
"native": {
"check": 0,
"status": 5,
"output": "",
"reason": "list-index-out-of-bounds"
}
}Append one more number
Keep the copy before either append. Add append(numbers, 10) after the existing append. Expected output is exactly 30\n3\n6\n, with normal status 0.
Append 10 after 8, then predict the sum and both lines about the copy.
Hint: Add a second append to numbers before the loop. Leave copied and the strict loop bound alone.
Show worked answer
The loop visits five integers and sums 2 + 4 + 6 + 8 + 10 to 30. copied was made before either append, so its length remains 3 and its element at index 2 remains 6. Only numbers changes. Formatting and explicit newlines produce the three lines; the entry returns status 0.
Execution: Browser and native · ASPL 0.7.0
- Read the example explanation in its lesson before running.
Input: No input lines are required.
fn start_main() -> i64 {
var numbers: list<i64> = [2, 4, 6];
let copied: list<i64> = numbers;
append(numbers, 8);
append(numbers, 10);
var index: i64 = 0;
var total: i64 = 0;
while (index < length(numbers)) {
total = total + numbers[index];
index = index + 1;
}
print(format_i64(total) + "\n");
print(format_i64(length(copied)) + "\n");
print(format_i64(copied[2]) + "\n");
return 0;
}
Expected outcomes
{
"browser": {
"check": "success",
"class": "success",
"output": "30\n3\n6\n",
"processStatus": 0
},
"native": {
"check": 0,
"status": 0,
"output": "30\n3\n6\n"
}
}Check your understanding
Explain why the last index is one less than length and why var is required for append. Predict what happens if the loop uses <= instead of <. No grade or progress is stored, and navigation stays open. Return to the Tutorial path or read the lists reference.