Something went wrong. Try again.
An experiment in static, refcounted, procedural language with ownership
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657type LinkedList[T]( tail: Option[&mut Node[T]], head: Option[Node[T]],)fn LinkedList.new[T]() LinkedList[T] = LinkedList(tail: Option.None, head: Option.None)type Node[T]( prev: Option[&mut Node[T]], data: T, next: Option[Node[T]],)fn Node.new[T](data: T) Node[T] = Node(prev: Option.None, data: data, next: Option.None)fn push-front[T](list: &mut LinkedList[T], data: T) unit = { let mut node = Node.new(data) let node-mut = &mut node match list.head := Option.Some(node) { Option.Some(old-head) -> { old-head.prev = Option.Some(node-mut) node-mut.next = Option.Some(old-head) } Option.None -> list.tail = Option.Some(node-mut) }}fn print(list: &LinkedList[int]) unit = { let mut curr = &list.head print("[") for { match curr { Option.Some(v) -> { curr = &v.next print("{v.data}") if curr.is-some() { print(", ") } } Option.None -> break } } println("]")}fn main() unit = { let mut list = LinkedList.new() list.push-front(3) list.push-front(2) list.push-front(1) list.print()}