2012-12-10 19:32:48 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-09-11 19:46:20 -05:00
|
|
|
extern mod std;
|
2011-08-24 19:24:58 -05:00
|
|
|
|
2012-09-05 14:32:05 -05:00
|
|
|
use std::list::*;
|
2011-08-24 19:24:58 -05:00
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
pure fn pure_length_go<T: Copy>(ls: @List<T>, acc: uint) -> uint {
|
2012-09-04 16:36:12 -05:00
|
|
|
match *ls { Nil => { acc } Cons(_, tl) => { pure_length_go(tl, acc + 1u) } }
|
2011-08-24 19:24:58 -05:00
|
|
|
}
|
|
|
|
|
2012-09-07 16:52:28 -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-07 16:52:28 -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-07 16:52:28 -05:00
|
|
|
fn safe_head<T: Copy>(ls: @List<T>) -> T {
|
2012-07-13 20:43:52 -05:00
|
|
|
assert is_not_empty(ls);
|
2012-08-01 19:30:05 -05:00
|
|
|
return head(ls);
|
2011-12-29 14:24:03 -06:00
|
|
|
}
|
2011-08-24 19:24:58 -05:00
|
|
|
|
|
|
|
fn main() {
|
2012-09-04 16:12:14 -05:00
|
|
|
let mylist = @Cons(@1u, @Nil);
|
2012-07-13 20:43:52 -05:00
|
|
|
assert (nonempty_list(mylist));
|
2011-09-02 17:34:58 -05:00
|
|
|
assert (*safe_head(mylist) == 1u);
|
|
|
|
}
|