added nth_update: 'a -> int * 'a list -> 'a list;
authorwenzelm
Mon, 04 May 1998 21:05:38 +0200
changeset 4893 df9d6eef16d5
parent 4892 0f80e924009d
child 4894 32187e0b8b48
added nth_update: 'a -> int * 'a list -> 'a list;
src/Pure/library.ML
--- a/src/Pure/library.ML	Mon May 04 21:05:14 1998 +0200
+++ b/src/Pure/library.ML	Mon May 04 21:05:38 1998 +0200
@@ -80,6 +80,7 @@
   val nth_elem: int * 'a list -> 'a
   val last_elem: 'a list -> 'a
   val split_last: 'a list -> 'a list * 'a
+  val nth_update: 'a -> int * 'a list -> 'a list
   val find_index: ('a -> bool) -> 'a list -> int
   val find_index_eq: ''a -> ''a list -> int
   val find_first: ('a -> bool) -> 'a list -> 'a option
@@ -446,6 +447,17 @@
   | split_last [x] = ([], x)
   | split_last (x :: xs) = apfst (cons x) (split_last xs);
 
+(*update nth element*)
+fun nth_update x (n, xs) =
+  let
+    val prfx = take (n, xs);
+    val sffx = drop (n, xs);
+  in
+    (case sffx of
+      [] => raise LIST "nth_update"
+    | _ :: sffx' => prfx @ (x :: sffx'))
+  end;
+
 (*find the position of an element in a list*)
 fun find_index pred =
   let fun find _ [] = ~1