不可变列表
不可变列表位于包 @list
中。
它可以是:
Empty
: 一个空列表More
: 一个元素和列表的其余部分。
可以用 @list.empty()
和 @list.construct(..)
来构造列表。还可以用 xs.prepend(x)
来往列表头部追加元素。
fn[A] count(list : @list.T[A]) -> UInt {
match list {
Empty => 0
More(_, tail~) => count(tail) + 1
}
}
///|
fn main {
let empty_list = @list.empty()
let list_1 = @list.construct(1, empty_list)
let list_2 = list_1.prepend(2)
let list_3 = empty_list.prepend(3)
let reversed_1 = list_1.rev()
let n = count(reversed_1)
}