2012-12-13 13:05:22 -08:00
|
|
|
// xfail-fast
|
|
|
|
|
2012-12-10 17:32:48 -08: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.
|
|
|
|
|
2013-05-20 17:07:24 -07:00
|
|
|
extern mod extra;
|
2011-08-24 17:24:58 -07:00
|
|
|
|
2013-05-20 17:07:24 -07:00
|
|
|
use extra::list::*;
|
2011-08-24 17:24:58 -07:00
|
|
|
|
2013-07-11 12:05:17 -07:00
|
|
|
fn pure_length_go<T:Clone>(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
|
|
|
}
|
|
|
|
|
2013-07-11 12:05:17 -07:00
|
|
|
fn pure_length<T:Clone>(ls: @List<T>) -> uint { pure_length_go(ls, 0u) }
|
2011-08-24 17:24:58 -07:00
|
|
|
|
2013-07-11 12:05:17 -07:00
|
|
|
fn nonempty_list<T:Clone>(ls: @List<T>) -> bool { pure_length(ls) > 0u }
|
2011-08-24 17:24:58 -07:00
|
|
|
|
2013-07-11 12:05:17 -07:00
|
|
|
fn safe_head<T:Clone>(ls: @List<T>) -> T {
|
2013-03-28 18:39:09 -07:00
|
|
|
assert!(!is_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
|
|
|
|
2013-02-01 19:43:17 -08:00
|
|
|
pub fn main() {
|
2012-09-04 14:12:14 -07:00
|
|
|
let mylist = @Cons(@1u, @Nil);
|
2013-03-28 18:39:09 -07:00
|
|
|
assert!((nonempty_list(mylist)));
|
2013-05-18 22:02:45 -04:00
|
|
|
assert_eq!(*safe_head(mylist), 1u);
|
2011-09-02 15:34:58 -07:00
|
|
|
}
|