စံကား

09

Lists

Keep a fixed number of values of one kind next to each other, and reach each one through its number.

A list holds a fixed number of values of one kind, next to each other in RAM. Each value sits in a cell, and a cell is reached through its number.

စံကား

Expected output

[၇၀၊ ၀၊ ၉၀]
၉၀
ZakaReads as
အကွက် ၃ ခုရှိသော ကိန်းပြည့် စာရင်း"an integer list that has three cells"
အမှတ်များ ၏ အကွက် ၀"cell 0 of the scores"
စာရင်း"list"
အကွက်"cell"

A list declared with no value starts with each cell at its default: ၀ for a number, မမှန် for a true/false value, ဗလာ for an address. စာရင်းအလွတ် ("blank list") writes the same start out in full.

List kinds

The count comes first, then the kind of each cell, then စာရင်း. The word ခု and a ၏ before စာရင်း are both optional, so these four kinds are the same:

Kind
အကွက် ၃ ခုရှိသော ကိန်းပြည့် စာရင်း
အကွက် ၃ ရှိသော ကိန်းပြည့် စာရင်း
အကွက် ၃ ခုရှိသော ကိန်းပြည့် ၏ စာရင်း
အကွက် ၃ ရှိသော ကိန်းပြည့် ၏ စာရင်း

Messages write this kind as [3]isize. The count is a whole number above zero, written as digits: the size of a list is fixed before the program runs.

When a declaration gives the list a value, the count can be left out. The value then tells it:

စံကား

Expected output

၃
၇၀
၈၅

The value ၇၀၊ ၈၅၊ ၉၀ တို့ပါသော စာရင်း is a list literal: the cells, with ၊ between them, then တို့ပါသော စာရင်း ("the list that holds these").

Cell numbers

ကိန်းများ ၏ အကွက် ၀ counts from 0, as programs count. ကိန်းများ ၏ ၁ ခုမြောက်အကွက် counts from 1, as people count: "the first cell".

WrittenCell
အမှတ်များ ၏ အကွက် ၀the first
အမှတ်များ ၏ ၁ ခုမြောက်အကွက်the first
အမှတ်များ ၏ အကွက် ၂the third
အမှတ်များ ၏ ၃ ခုမြောက်အကွက်the third

အမှတ်များ ၏ အကွက်အရေအတွက် ("the cell count of the scores") gives the number of cells, a လက္ခဏာမဲ့ ကိန်းပြည့်. A cell number outside the list stops the program: see List mistakes.

Lists of other kinds

Cells can hold each kind, and a list prints its cells between [ and ]:

စံကား

Expected output

[၀.၀၊ ၀.၀]
[မမှန်၊ မမှန်၊ မမှန်]
[က၊ ခ]

Cells in operator expressions

The number after အကွက် takes the whole operator expression that follows it, just as ပ + ၁ ၏ တန်ဖိုး reads through ပ + ၁. To use a cell inside a sum, put the cell in parentheses:

စံကား

Expected output

၂၀
၁၁

Walking a list

A counted loop visits each cell. The cell count, in parentheses, gives the last number:

စံကား

Expected output

၄၃

Hands on

  • Declare a list of five decimal numbers, set the middle one, and print the list.
  • Print the last cell of a list, once counting from 0 and once counting from 1.
  • Find the largest value in a list with a counted loop.

Next: Passing and comparing lists.