rust/src/test/run-pass/non-boolean-pure-fns.rs

23 lines
561 B
Rust
Raw Normal View History

2011-08-24 19:24:58 -05:00
use std;
import std::list::*;
2012-09-04 16:12:14 -05:00
pure fn pure_length_go<T: copy>(ls: @List<T>, acc: uint) -> uint {
match *ls { nil => { acc } Cons(_, tl) => { pure_length_go(tl, acc + 1u) } }
2011-08-24 19:24:58 -05:00
}
2012-09-04 16:12:14 -05:00
pure fn pure_length<T: copy>(ls: @List<T>) -> uint { pure_length_go(ls, 0u) }
2011-08-24 19:24:58 -05:00
2012-09-04 16:12:14 -05:00
pure fn nonempty_list<T: copy>(ls: @List<T>) -> bool { pure_length(ls) > 0u }
2011-08-24 19:24:58 -05:00
2012-09-04 16:12:14 -05:00
fn safe_head<T: copy>(ls: @List<T>) -> T {
assert is_not_empty(ls);
2012-08-01 19:30:05 -05:00
return head(ls);
}
2011-08-24 19:24:58 -05:00
fn main() {
2012-09-04 16:12:14 -05:00
let mylist = @Cons(@1u, @Nil);
assert (nonempty_list(mylist));
2011-09-02 17:34:58 -05:00
assert (*safe_head(mylist) == 1u);
}