diff options
| author | Lenny222 <github@kudling.de> | 2011-12-29 21:24:03 +0100 |
|---|---|---|
| committer | Lenny222 <github@kudling.de> | 2011-12-29 21:24:03 +0100 |
| commit | d07c6e8a0ede3114ebfd8c3ea6cc161cf009f072 (patch) | |
| tree | 986f6ce8c40d0c363a470e03349c7a23088ec811 /src/libstd | |
| parent | 816b0ac8ae3444a81e60e18a05430bb869ba4b3b (diff) | |
| download | rust-d07c6e8a0ede3114ebfd8c3ea6cc161cf009f072.tar.gz rust-d07c6e8a0ede3114ebfd8c3ea6cc161cf009f072.zip | |
list: use predicate to enforce non-empty requirement
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/list.rs | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/src/libstd/list.rs b/src/libstd/list.rs index c6a24d275eb..f6b46ed7d2e 100644 --- a/src/libstd/list.rs +++ b/src/libstd/list.rs @@ -98,6 +98,27 @@ fn has<copy T>(ls: list<T>, elt: T) -> bool { } /* +Function: is_empty + +Returns true if the list is empty. +*/ +pure fn is_empty<copy T>(ls: list<T>) -> bool { + alt ls { + nil. { true } + _ { false } + } +} + +/* +Function: is_not_empty + +Returns true if the list is not empty. +*/ +pure fn is_not_empty<copy T>(ls: list<T>) -> bool { + ret !is_empty(ls); +} + +/* Function: len Returns the length of a list @@ -112,8 +133,11 @@ Function: tail Returns all but the first element of a list */ -pure fn tail<copy T>(ls: list<T>) -> list<T> { - alt ls { cons(_, tl) { ret *tl; } nil. { fail "list empty" } } +pure fn tail<copy T>(ls: list<T>) : is_not_empty(ls) -> list<T> { + alt ls { + cons(_, tl) { ret *tl; } + nil. { fail "list empty" } + } } /* @@ -121,8 +145,11 @@ Function: head Returns the first element of a list */ -pure fn head<copy T>(ls: list<T>) -> T { - alt ls { cons(hd, _) { ret hd; } nil. { fail "list empty" } } +pure fn head<copy T>(ls: list<T>) : is_not_empty(ls) -> T { + alt ls { + cons(hd, _) { ret hd; } + nil. { fail "list empty" } + } } /* |
