09Lists
Passing and comparing lists
Lists are values, so each assignment and argument duplicates the cells; lend a list through its address.
A list is a value, like an integer. Setting one list to another duplicates each cell, and the two lists stand apart afterwards:
Expected output
[၁၊ ၂၊ ၃]
[၁၀၀၊ ၂၊ ၃]
Lists as parameters
A list parameter gets a duplicate of the list passed in, just as an integer parameter does. A change inside the function remains inside:
Expected output
[၀၊ ၂၊ ၃]
[၁၊ ၂၊ ၃]
To let a function change the caller's list, pass its address, as with the caller's
variables. The parameter kind is the list kind with
လိပ်စာ after it and ငှားထားသော in front, since the function borrows the caller's
list. The cells are reached through it with အကွက် as before:
Expected output
[၂၊ ၄၊ ၆]
Passing the address also saves duplicating a large list.
A function cannot give back a list as its result. Pass the address of a list to fill instead.
List literals as arguments
A list literal takes each value before its တို့ပါသော စာရင်း, back to the last ၊ that
starts the argument list. Put it in parentheses when another argument comes before it:
Expected output
၁၀၆
Comparing lists
== and !=, and the verbs ညီ and မညီ, compare two lists of the same kind, one cell
at a time:
Expected output
မှန်
မမှန်
မှန်
Lists of different kinds, such as [3]isize and [2]isize, do not compare. Lists have
no order, so < and > do not take them.
Hands on
- Write a function that takes the address of a list of integers and adds 1 to each cell.
- Duplicate a list, change the duplicate, and compare the two.
Next: Lists of lists.