aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/ur/list.ur
diff options
context:
space:
mode:
authorGravatar Adam Chlipala <adam@chlipala.net>2011-12-11 15:02:55 -0500
committerGravatar Adam Chlipala <adam@chlipala.net>2011-12-11 15:02:55 -0500
commit4f67fe6cd314ad3ca1286766f1f9f3d8c5084d48 (patch)
tree7d5004cf0d36df0abdb259ed724d4cb14f2ac8e6 /lib/ur/list.ur
parent02f3311b6fd7af07e63b5a0df2598b148783fd61 (diff)
Some new List functions, based on code by Ron de Bruijn
Diffstat (limited to 'lib/ur/list.ur')
-rw-r--r--lib/ur/list.ur31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/ur/list.ur b/lib/ur/list.ur
index 6a754287..2d5addc9 100644
--- a/lib/ur/list.ur
+++ b/lib/ur/list.ur
@@ -267,6 +267,18 @@ fun foldlMi [m] (_ : monad m) [a] [b] f =
foldlMi' 0
end
+fun filterM [m] (_ : monad m) [a] (p : a -> m bool) =
+ let
+ fun filterM' (acc : list a) (xs : list a) : m (list a) =
+ case xs of
+ [] => return (rev acc)
+ | x :: xs =>
+ c <- p x;
+ filterM' (if c then x :: acc else acc) xs
+ in
+ filterM' []
+ end
+
fun all [m] f =
let
fun all' ls =
@@ -393,3 +405,22 @@ fun assocAdd [a] [b] (_ : eq a) (x : a) (y : b) (ls : t (a * b)) =
fun recToList [a ::: Type] [r ::: {Unit}] (fl : folder r)
= @foldUR [a] [fn _ => list a] (fn [nm ::_] [rest ::_] [[nm] ~ rest] x xs =>
x :: xs) [] fl
+
+fun take [a] (n : int) (xs : list a) : list a =
+ if n <= 0 then
+ []
+ else
+ case xs of
+ [] => []
+ | x :: xs => x :: take (n-1) xs
+
+fun drop [a] (n : int) (xs : list a) : list a =
+ if n <= 0 then
+ xs
+ else
+ case xs of
+ [] => []
+ | x :: xs => drop (n-1) xs
+
+fun splitAt [a] (n : int) (xs : list a) : list a * list a =
+ (take n xs, drop n xs)