intermediate language-deep-dive

List Operations

Lists are Clean Language's sequence type. They grow dynamically, hold elements of one type, and have a predictable set of operations. Once you know the list pattern — add, access, iterate — you can handle any collection.

Part of: How Clean works

Create, add, access, and measure a list:

start:
	list<string> fruits = []
	fruits.add("apple")
	fruits.add("banana")
	fruits.add("cherry")

	print("Count: " + fruits.length().toString())
	print("First: " + fruits[0])
	print("Last: " + fruits[fruits.length() - 1])
	print("Has banana: " + fruits.contains("banana").toString())
	print("Has grape: " + fruits.contains("grape").toString())
Count: 3
First: apple
Last: cherry
Has banana: true
Has grape: false

Start with an empty list [] and specify the element type in the declaration. .add() appends to the end. Index access [n] gets a specific element (starting at 0). .length() returns the count. .contains() returns a boolean — use it directly in if conditions.

Build lists dynamically and search through them:

start:
	list<integer> squares = []
	iterate n in 1 to 8
		squares.add(n * n)

	string target = "36"
	integer found_at = -1
	iterate i in 0 to squares.length() - 1
		if squares[i].toString() == target
			found_at = i

	if found_at >= 0
		print(target + " found at index " + found_at.toString())
	else
		print(target + " not found")
36 found at index 5

Build a list dynamically by calling .add() inside an iterate. Search it by iterating with an index, checking each element, and recording the position. This count-then-iterate pattern works for any list of any size.

  • Declare as list<T> where T is the element type: list<string>, list<integer>
  • .add(value) appends to the end of the list
  • [n] accesses element at index n — first is [0], last is [.length() - 1]
  • .contains(value) returns boolean — safe to use directly in if conditions
📚 → Book 1, Chapter 3 : The String Toolkit →
Copied!