Add list function 'has'.
This commit is contained in:
parent
40fe44d23e
commit
3375b8fba2
2 changed files with 25 additions and 0 deletions
|
@ -52,6 +52,19 @@ fn find[T,U](&list[T] ls,
|
|||
}
|
||||
}
|
||||
|
||||
fn has[T](&list[T] ls, &T elt) -> bool {
|
||||
alt(ls) {
|
||||
case (cons[T](?hd, ?tl)) {
|
||||
if (elt == hd) {
|
||||
ret true;
|
||||
} else {
|
||||
be has(*tl, elt);
|
||||
}
|
||||
}
|
||||
case (nil[T]) { ret false; }
|
||||
}
|
||||
}
|
||||
|
||||
fn length[T](&list[T] ls) -> uint {
|
||||
fn count[T](&T t, &uint u) -> uint {
|
||||
ret u + 1u;
|
||||
|
|
|
@ -43,6 +43,17 @@ fn test_find_fail() {
|
|||
assert (res == option::none[int]);
|
||||
}
|
||||
|
||||
fn test_has() {
|
||||
auto l = from_vec([5, 8 ,6]);
|
||||
auto empty = list::nil[int];
|
||||
|
||||
assert (list::has(l, 5));
|
||||
assert (!list::has(l, 7));
|
||||
assert (list::has(l, 8));
|
||||
|
||||
assert (!list::has(empty, 5));
|
||||
}
|
||||
|
||||
fn test_length() {
|
||||
auto l = from_vec([0, 1, 2]);
|
||||
assert (list::length(l) == 3u);
|
||||
|
@ -54,4 +65,5 @@ fn main() {
|
|||
test_find_success();
|
||||
test_find_fail();
|
||||
test_length();
|
||||
test_has();
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue