All examplesLanguage Install Aner
A linked list
Build nullable links and use methods to traverse and mutate a reference structure.
Download .aner source// A linked list uses nullable links and mutable object fields.
// let fixes the binding; methods may still update the object's var fields.
class Node {
public let value: Int64
public var next: Node?
fn get() {
return value
}
}
class LinkedList {
public var head: Node?
fn push(value: Int64) {
head = Node(value: value, next: head)
}
fn len() {
var count = 0
var cursor = head
while cursor != null {
let node = cursor.unwrap()
count = count + 1
cursor = node.next
}
return count
}
fn sum() {
var total = 0
var cursor = head
while cursor != null {
let node = cursor.unwrap()
total = total + node.value
cursor = node.next
}
return total
}
}
let numbers = LinkedList(head: null)
numbers.push(30)
numbers.push(20)
numbers.push(10)
print("Length")
print(numbers.len())
print("Sum")
print(numbers.sum())
print("First value")
print(numbers.head.unwrap().get())
let alias = numbers
print("Aliases share identity")
print(alias == numbers)
alias.push(5)
print("Length after an update through the alias")
print(numbers.len())
Make it your experiment.
With Aner installed, save this program as examples/oop_linked_list.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.
aner check examples/oop_linked_list.aner
aner run examples/oop_linked_list.anerThe aner command must be on your PATH. Follow the installation guide if your terminal cannot find it.
Iris and Wine are teaching datasets with their own attribution. Example outcomes are not comparative benchmarks or evidence of clinical validity.
Dataset sources & attribution