about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorBrian Anderson <andersrb@gmail.com>2012-01-03 12:18:53 -0800
committerBrian Anderson <andersrb@gmail.com>2012-01-03 12:18:53 -0800
commit513a263e81eff098762e1531bc4d13366da0e554 (patch)
treeeff25c9c3e15e57d4d308f0a6028b2d625fb1b8a /src/libstd
parent4e88d5ae92af1ebfb1d23e027db7133c669cca69 (diff)
parentab2a643f278d09c3fcc140ecb4c7cecbf1aff3c4 (diff)
downloadrust-513a263e81eff098762e1531bc4d13366da0e554.tar.gz
rust-513a263e81eff098762e1531bc4d13366da0e554.zip
Merge pull request #1392 from Lenny222/list
list: add "is_not_empty" requirement to "head" and "tail" (analogous to "vec")
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/list.rs35
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" }
+    }
 }
 
 /*