Traverse from the beginning of a singly-linked list to the end.



See also

 Array
 Associative array: Creation, Iteration
 Collections
 Compound data type
 Doubly-linked list: Definition, Element definition, Element insertion, List Traversal, Element Removal
 Linked list
 Queue: Definition, Usage
 Set
 Singly-linked list: Element definition, Element insertion, List Traversal, Element Removal
 Stack


type List = Cons(value, tail) or Nil()
    with Lookup
 
static func List.FromArray(xs) {
    var list = List.Nil()
    var len = xs.Length()
 
    for i in (len-1)^-1..0 {
        list = List.Cons(xs[i], list)
    }
 
    return list
}
 
func List.Iterate() {
    var xs = this
    do {
        match xs {
            Cons(value, tail) => {
                yield value
                xs = tail
            },
            Nil() => {
                yield break
            }
        }
 
    } while true
}
 
var xs = List.FromArray([1..10])
 
for x in xs {
    print(x)
}