Lists and records are programmatic structures that hold data while the program is running. The data can be added, accessed or deleted by specific coding semantics. As with scalar variables, lists and records are also variables and have a key by which the system can identify them, and functionalities: assign, get value and pass to functions.
Lists
List is an ordered collection of items. Each item can be scalar variable, list or record. If all items of a list are of the same variable type, than it can be traversed by the list iterator, where in each iteration an item of the list can be accessed in the iteration block. A unique feature of Till is that when declaring a variable that its key (name) has at least two characters and ends with “s”, it is considered automatically as list. Otherwise, it is needed to append rectangular brackets to the key. Also, if the declaration line includes list of items, it is not mandatory to separate them by commas.
Declarations:
nums
modes 'ID' 'ORDER'
data []
data1 data2 data3 []
shapeslist [ 'Rectangle' 'Triangle' 'Polygon' ]
listas 'a1' 'a2'
listbs 'b1', 'b2'
listoflists listas listbs
- declare empty list variable “nums”
- declare list “modes” that has two static string items
- declare empty list “data”
- declare some lists in one declaration line
- declare list “shapeslist” that has three items
- declare lists “listas” and “listbs”, that will be used as items in the declaration of list “listoflists”
Access, set and remove:
print modes 0
position 1
item2 modes position
modes 0 'JD'
rem modes ->
modes 1 ->
modes <- 'KD'
modes 1 <- 'LD'
modes ->->
- print first item of “modes”
- “position” equals 1
- “item2” gets the item at position “position” in list “modes”
- set the value of the item of “modes” in position 0 to ‘JD’
- if “modes” has at least one item, remove the first item and assign it to variable “rem”, otherwise, “rem” will be NULL
- remove the second item of “modes”
- add string “KD” to “modes”
- insert string “LD” at position 1
- remove all items of “modes”, thus clearing the list
Records
Record is a collection of identified items (also known as dictionary, mapping and hash map). Each item has a key by which it can be identified and accessed, and a value that can be scalar, list or record. All of record items are either declared inside curly brackets or automatically defined upon item addition. Any line of declaration of record(s) must end with curly brackets.
Declarations:
rec {}
num 10
result { ok \t val num }
rec1 rec2 {}
- declare empty record variable “rec”
- declare variable “num” with value 10
- declare record “result” that has two items: “ok” (value TRUE) and “val” (value of variable “num”)
- declare some records in the same declaration line
Access, set and remove:
print result.ok
result.val 1
val result 'val'
rec1 <- 'newkey' 'newval'
rem rec1 -> 'newkey'
- print (value of) property “ok” of “result” (dot-notation access)
- set property “val” of “result” to 1
- variable “val” gets property “val” of “result” (dotless access)
- add to record “rec1”, a new item “newkey” with value “newval”
- remove item “newkey” from record “rec1”, and assign its value to variable “rem”
In the reference there are various programs that demonstrate how to work with lists and records. These programs go through all of programming scenarios including declarations, assignments, passing to functions, and assign by function result.