diff lib/ur/list.ur @ 845:6725d73c3c31

Mark current as effectful; add List functions
author Adam Chlipala <adamc@hcoop.net>
date Tue, 09 Jun 2009 11:12:34 -0400
parents 74a1e3bdf430
children 0d30e6338c65
line wrap: on
line diff
--- a/lib/ur/list.ur	Sun Jun 07 16:45:00 2009 -0400
+++ b/lib/ur/list.ur	Tue Jun 09 11:12:34 2009 -0400
@@ -10,6 +10,16 @@
                   mkShow show'
               end
 
+fun foldl [a] [b] f =
+    let
+        fun foldl' acc ls =
+            case ls of
+                [] => acc
+              | x :: ls => foldl' (f x acc) ls
+    in
+        foldl'
+    end
+
 val rev = fn [a] =>
              let
                  fun rev' acc (ls : list a) =
@@ -123,20 +133,6 @@
         fold []
     end
 
-fun assoc [a] [b] (_ : eq a) (x : a) =
-    let
-        fun assoc' ls =
-            case ls of
-                [] => None
-              | (y, z) :: ls =>
-                if x = y then
-                    Some z
-                else
-                    assoc' ls
-    in
-        assoc'
-    end
-
 fun search [a] [b] f =
     let
         fun search' ls =
@@ -183,3 +179,22 @@
     in
         app'
     end
+
+fun assoc [a] [b] (_ : eq a) (x : a) =
+    let
+        fun assoc' (ls : list (a * b)) =
+            case ls of
+                [] => None
+              | (y, z) :: ls =>
+                if x = y then
+                    Some z
+                else
+                    assoc' ls
+    in
+        assoc'
+    end
+
+fun assocAdd [a] [b] (_ : eq a) (x : a) (y : b) (ls : t (a * b)) =
+    case assoc x ls of
+        None => (x, y) :: ls
+      | Some _ => ls