2011-10-26 13:28:23 -05:00
|
|
|
/*
|
|
|
|
Module: list
|
|
|
|
|
|
|
|
A standard linked list
|
|
|
|
*/
|
|
|
|
|
2011-09-12 18:13:28 -05:00
|
|
|
import option::{some, none};
|
2010-10-18 16:35:44 -05:00
|
|
|
|
2011-10-26 13:28:23 -05:00
|
|
|
/* Section: Types */
|
|
|
|
|
|
|
|
/*
|
|
|
|
Tag: list
|
|
|
|
*/
|
|
|
|
tag list<T> {
|
|
|
|
/* Variant: cons */
|
|
|
|
cons(T, @list<T>);
|
|
|
|
/* Variant: nil */
|
|
|
|
nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Section: Operations */
|
2010-10-16 00:09:09 -05:00
|
|
|
|
2011-10-26 13:28:23 -05:00
|
|
|
/*
|
|
|
|
Function: from_vec
|
|
|
|
|
|
|
|
Create a list from a vector
|
|
|
|
*/
|
2011-10-28 15:23:50 -05:00
|
|
|
fn from_vec<T>(v: [mutable? T]) -> list<T> {
|
2011-08-12 12:56:57 -05:00
|
|
|
let l = nil::<T>;
|
2011-05-21 23:11:28 -05:00
|
|
|
// 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
|
|
|
|
2011-08-12 12:56:57 -05:00
|
|
|
for item: T in vec::reversed(v) { l = cons::<T>(item, @l); }
|
2011-05-21 23:11:28 -05:00
|
|
|
ret l;
|
|
|
|
}
|
|
|
|
|
2011-10-26 13:28:23 -05:00
|
|
|
/*
|
|
|
|
Function: foldl
|
|
|
|
|
|
|
|
Left fold
|
|
|
|
|
|
|
|
Applies `f` to the first argument in the list and `u`, then applies
|
|
|
|
`f` to the second argument and the result of the previous call,
|
|
|
|
and so on, returning the accumulated result.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
|
|
|
ls_ - The list to fold
|
|
|
|
u - The initial value
|
|
|
|
f - The function to apply
|
|
|
|
*/
|
2011-10-25 08:56:55 -05:00
|
|
|
fn foldl<T, U>(ls_: list<T>, u: U, f: block(T, U) -> U) -> U {
|
2011-07-27 07:19:39 -05:00
|
|
|
let accum: U = u;
|
|
|
|
let ls = ls_;
|
|
|
|
while true {
|
|
|
|
alt ls {
|
|
|
|
cons(hd, tl) { accum = f(hd, accum); ls = *tl; }
|
|
|
|
nil. { break; }
|
2011-05-21 21:11:42 -05:00
|
|
|
}
|
2010-10-18 16:35:44 -05:00
|
|
|
}
|
2011-06-15 14:38:43 -05:00
|
|
|
ret accum;
|
2010-10-18 16:35:44 -05:00
|
|
|
}
|
|
|
|
|
2011-10-26 13:28:23 -05:00
|
|
|
/*
|
|
|
|
Function: find
|
|
|
|
|
|
|
|
Search for an element that matches a given predicate
|
|
|
|
|
|
|
|
Apply function `f` to each element of `v`, starting from the first.
|
|
|
|
When function `f` returns true then an option containing the element
|
|
|
|
is returned. If `f` matches no elements then none is returned.
|
|
|
|
*/
|
2011-10-25 08:56:55 -05:00
|
|
|
fn find<T, U>(ls_: list<T>, f: block(T) -> option::t<U>) -> option::t<U> {
|
2011-07-27 07:19:39 -05:00
|
|
|
let ls = ls_;
|
|
|
|
while true {
|
|
|
|
alt ls {
|
|
|
|
cons(hd, tl) {
|
|
|
|
alt f(hd) { none. { ls = *tl; } some(rs) { ret some(rs); } }
|
|
|
|
}
|
|
|
|
nil. { break; }
|
2010-10-18 16:35:44 -05:00
|
|
|
}
|
|
|
|
}
|
2011-06-15 14:38:43 -05:00
|
|
|
ret none;
|
2010-10-18 16:35:44 -05:00
|
|
|
}
|
|
|
|
|
2011-10-26 13:28:23 -05:00
|
|
|
/*
|
|
|
|
Function: has
|
|
|
|
|
|
|
|
Returns true if a list contains an element with the given value
|
|
|
|
*/
|
2011-10-25 08:56:55 -05:00
|
|
|
fn has<T>(ls_: list<T>, elt: T) -> bool {
|
2011-07-27 07:19:39 -05:00
|
|
|
let ls = ls_;
|
|
|
|
while true {
|
|
|
|
alt ls {
|
|
|
|
cons(hd, tl) { if elt == hd { ret true; } else { ls = *tl; } }
|
2011-08-09 19:36:07 -05:00
|
|
|
nil. { break; }
|
2011-05-25 11:54:43 -05:00
|
|
|
}
|
|
|
|
}
|
2011-08-09 19:36:07 -05:00
|
|
|
ret false;
|
2011-05-25 11:54:43 -05:00
|
|
|
}
|
|
|
|
|
2011-10-26 13:28:23 -05:00
|
|
|
/*
|
|
|
|
Function: length
|
|
|
|
|
|
|
|
Returns the length of a list
|
|
|
|
*/
|
2011-10-25 08:56:55 -05:00
|
|
|
fn length<T>(ls: list<T>) -> uint {
|
2011-10-06 05:26:12 -05:00
|
|
|
fn count<T>(_t: T, &&u: uint) -> uint { ret u + 1u; }
|
2011-10-18 17:07:40 -05:00
|
|
|
ret foldl(ls, 0u, bind count(_, _));
|
2010-10-18 16:35:44 -05:00
|
|
|
}
|
2010-10-16 00:09:09 -05:00
|
|
|
|
2011-10-26 13:28:23 -05:00
|
|
|
/*
|
|
|
|
Function: cdr
|
|
|
|
|
|
|
|
Returns all but the first element of a list
|
|
|
|
*/
|
2011-10-25 08:56:55 -05:00
|
|
|
fn cdr<T>(ls: list<T>) -> list<T> {
|
2011-08-19 17:16:48 -05:00
|
|
|
alt ls { cons(_, tl) { ret *tl; } nil. { fail "list empty" } }
|
2011-08-09 19:36:07 -05:00
|
|
|
}
|
2011-05-09 05:40:09 -05:00
|
|
|
|
2011-10-26 13:28:23 -05:00
|
|
|
/*
|
|
|
|
Function: car
|
|
|
|
|
|
|
|
Returns the first element of a list
|
|
|
|
*/
|
2011-10-25 08:56:55 -05:00
|
|
|
fn car<T>(ls: list<T>) -> T {
|
2011-08-19 17:16:48 -05:00
|
|
|
alt ls { cons(hd, _) { ret hd; } nil. { fail "list empty" } }
|
2011-08-09 19:36:07 -05:00
|
|
|
}
|
2011-05-26 21:05:23 -05:00
|
|
|
|
2011-10-26 13:28:23 -05:00
|
|
|
/*
|
|
|
|
Function: append
|
|
|
|
|
|
|
|
Appends one list to another
|
|
|
|
*/
|
2011-10-25 08:56:55 -05:00
|
|
|
fn append<T>(l: list<T>, m: list<T>) -> list<T> {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt l {
|
|
|
|
nil. { ret m; }
|
2011-08-19 17:16:48 -05:00
|
|
|
cons(x, xs) { let rest = append(*xs, m); ret cons(x, @rest); }
|
2011-05-26 21:05:23 -05:00
|
|
|
}
|
|
|
|
}
|
2011-08-09 19:36:07 -05:00
|
|
|
|
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:
|