rust/src/lib/list.rs

89 lines
2.2 KiB
Rust
Raw Normal View History

import option::some;
import option::none;
2010-10-18 16:35:44 -05:00
// FIXME: It would probably be more appealing to define this as
// type list[T] = rec(T hd, option[@list[T]] tl), but at the moment
// our recursion rules do not permit that.
tag list[T] {
cons(T, @list[T]);
nil;
}
2011-05-21 23:11:28 -05:00
fn from_vec[T](vec[T] v) -> list[T] {
auto l = nil[T];
// FIXME: This would be faster and more space efficient if it looped over
// a reverse vector iterator. Unfortunately generic iterators seem not to
// work yet.
for (T item in vec::reversed(v)) {
l = cons[T](item, @l);
}
ret l;
}
fn foldl[T,U](&list[T] ls, &U u, fn(&T t, &U u) -> U f) -> U {
2011-05-21 21:11:42 -05:00
alt(ls) {
case (cons[T](?hd, ?tl)) {
auto u_ = f(hd, u);
// FIXME: should use 'be' here, not 'ret'. But parametric
// tail calls currently don't work.
ret foldl[T,U](*tl, u_, f);
}
case (nil[T]) {
ret u;
}
2010-10-18 16:35:44 -05:00
}
2011-05-21 21:11:42 -05:00
fail; // TODO: remove me when exhaustiveness checking works
2010-10-18 16:35:44 -05:00
}
fn find[T,U](&list[T] ls,
(fn(&T) -> option::t[U]) f) -> option::t[U] {
2011-05-21 21:11:42 -05:00
alt(ls) {
case (cons[T](?hd, ?tl)) {
alt (f(hd)) {
case (none[U]) {
// FIXME: should use 'be' here, not 'ret'. But parametric
// tail calls currently don't work.
ret find[T,U](*tl, f);
}
case (some[U](?res)) {
ret some[U](res);
}
2010-10-18 16:35:44 -05:00
}
}
2011-05-21 21:11:42 -05:00
case (nil[T]) {
ret none[U];
}
2010-10-18 16:35:44 -05:00
}
2011-05-21 21:11:42 -05:00
fail; // TODO: remove me when exhaustiveness checking works
2010-10-18 16:35:44 -05:00
}
fn length[T](&list[T] ls) -> uint {
2011-05-21 21:11:42 -05:00
fn count[T](&T t, &uint u) -> uint {
ret u + 1u;
}
ret foldl[T,uint](ls, 0u, bind count[T](_, _));
2010-10-18 16:35:44 -05:00
}
fn cdr[T](&list[T] ls) -> list[T] {
alt (ls) {
case (cons[T](_, ?tl)) {ret *tl;}
}
}
fn car[T](&list[T] ls) -> T {
alt (ls) {
case (cons[T](?hd, _)) {ret hd;}
}
}
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End: