2011-08-24 17:24:58 -07:00
|
|
|
use std;
|
|
|
|
|
2012-09-05 12:32:05 -07:00
|
|
|
use std::list::*;
|
2011-08-24 17:24:58 -07:00
|
|
|
|
2012-09-07 14:52:28 -07:00
|
|
|
pure fn pure_length_go<T: Copy>(ls: @List<T>, acc: uint) -> uint {
|
2012-09-04 14:36:12 -07:00
|
|
|
match *ls { Nil => { acc } Cons(_, tl) => { pure_length_go(tl, acc + 1u) } }
|
2011-08-24 17:24:58 -07:00
|
|
|
}
|
|
|
|
|
2012-09-07 14:52:28 -07:00
|
|
|
pure fn pure_length<T: Copy>(ls: @List<T>) -> uint { pure_length_go(ls, 0u) }
|
2011-08-24 17:24:58 -07:00
|
|
|
|
2012-09-07 14:52:28 -07:00
|
|
|
pure fn nonempty_list<T: Copy>(ls: @List<T>) -> bool { pure_length(ls) > 0u }
|
2011-08-24 17:24:58 -07:00
|
|
|
|
2012-09-07 14:52:28 -07:00
|
|
|
fn safe_head<T: Copy>(ls: @List<T>) -> T {
|
2012-07-13 18:43:52 -07:00
|
|
|
assert is_not_empty(ls);
|
2012-08-01 17:30:05 -07:00
|
|
|
return head(ls);
|
2011-12-29 21:24:03 +01:00
|
|
|
}
|
2011-08-24 17:24:58 -07:00
|
|
|
|
|
|
|
fn main() {
|
2012-09-04 14:12:14 -07:00
|
|
|
let mylist = @Cons(@1u, @Nil);
|
2012-07-13 18:43:52 -07:00
|
|
|
assert (nonempty_list(mylist));
|
2011-09-02 15:34:58 -07:00
|
|
|
assert (*safe_head(mylist) == 1u);
|
|
|
|
}
|