2011-06-15 13:19:50 -05:00
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
import option::some;
|
|
|
|
import option::none;
|
2010-10-18 16:35:44 -05:00
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
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.
|
2011-06-15 13:19:50 -05:00
|
|
|
tag list[T] { cons(T, @list[T]); nil; }
|
2010-10-16 00:09:09 -05:00
|
|
|
|
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.
|
2011-06-15 13:19:50 -05:00
|
|
|
|
|
|
|
for (T item in vec::reversed(v)) { l = cons[T](item, @l); }
|
2011-05-21 23:11:28 -05:00
|
|
|
ret l;
|
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
fn foldl[T, U](&list[T] ls, &U u, fn(&T, &U) -> U f) -> U {
|
|
|
|
alt (ls) {
|
2011-05-30 23:13:37 -05:00
|
|
|
case (cons(?hd, ?tl)) {
|
2011-05-21 21:11:42 -05:00
|
|
|
auto u_ = f(hd, u);
|
2011-06-15 13:19:50 -05:00
|
|
|
be foldl[T, U](*tl, u_, f);
|
2011-05-21 21:11:42 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
case (nil) { ret u; }
|
2010-10-18 16:35:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
fn find[T, U](&list[T] ls, fn(&T) -> option::t[U] f) -> option::t[U] {
|
|
|
|
alt (ls) {
|
2011-05-30 23:13:37 -05:00
|
|
|
case (cons(?hd, ?tl)) {
|
2011-05-21 21:11:42 -05:00
|
|
|
alt (f(hd)) {
|
2011-06-15 13:19:50 -05:00
|
|
|
case (none) { be find[T, U](*tl, f); }
|
|
|
|
case (some(?res)) { ret some[U](res); }
|
2010-10-18 16:35:44 -05:00
|
|
|
}
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
case (nil) { ret none[U]; }
|
2010-10-18 16:35:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-25 11:54:43 -05:00
|
|
|
fn has[T](&list[T] ls, &T elt) -> bool {
|
2011-06-15 13:19:50 -05:00
|
|
|
alt (ls) {
|
2011-05-30 23:13:37 -05:00
|
|
|
case (cons(?hd, ?tl)) {
|
2011-06-15 13:19:50 -05:00
|
|
|
if (elt == hd) { ret true; } else { be has(*tl, elt); }
|
2011-05-25 11:54:43 -05:00
|
|
|
}
|
2011-05-30 23:13:37 -05:00
|
|
|
case (nil) { ret false; }
|
2011-05-25 11:54:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-18 16:35:44 -05:00
|
|
|
fn length[T](&list[T] ls) -> uint {
|
2011-06-15 13:19:50 -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
|
|
|
}
|
2010-10-16 00:09:09 -05:00
|
|
|
|
2011-05-09 05:40:09 -05:00
|
|
|
fn cdr[T](&list[T] ls) -> list[T] {
|
2011-06-15 13:19:50 -05:00
|
|
|
alt (ls) { case (cons(_, ?tl)) { ret *tl; } }
|
2011-05-09 05:40:09 -05:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
fn car[T](&list[T] ls) -> T { alt (ls) { case (cons(?hd, _)) { ret hd; } } }
|
2011-05-26 21:05:23 -05:00
|
|
|
|
|
|
|
fn append[T](&list[T] l, &list[T] m) -> list[T] {
|
|
|
|
alt (l) {
|
2011-06-15 13:19:50 -05:00
|
|
|
case (nil) { ret m; }
|
2011-05-30 23:13:37 -05:00
|
|
|
case (cons(?x, ?xs)) {
|
2011-05-26 21:05:23 -05:00
|
|
|
let list[T] rest = append[T](*xs, m);
|
|
|
|
ret cons[T](x, @rest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-16 00:09:09 -05:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust;
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-06-15 14:01:19 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2010-10-16 00:09:09 -05:00
|
|
|
// End:
|