src/Pure/library.ML
changeset 19691 dd9ccb370f52
parent 19644 0b01436e1843
child 19799 666de5708ae8
equal deleted inserted replaced
19690:8c03cadf9886 19691:dd9ccb370f52
   286   val foldr: ('a * 'b -> 'b) -> 'a list * 'b -> 'b
   286   val foldr: ('a * 'b -> 'b) -> 'a list * 'b -> 'b
   287   val take: int * 'a list -> 'a list
   287   val take: int * 'a list -> 'a list
   288   val drop: int * 'a list -> 'a list
   288   val drop: int * 'a list -> 'a list
   289   val last_elem: 'a list -> 'a
   289   val last_elem: 'a list -> 'a
   290   val seq: ('a -> unit) -> 'a list -> unit
   290   val seq: ('a -> unit) -> 'a list -> unit
   291   val partition: ('a -> bool) -> 'a list -> 'a list * 'a list
       
   292 end;
   291 end;
   293 
   292 
   294 structure Library: LIBRARY =
   293 structure Library: LIBRARY =
   295 struct
   294 struct
   296 
   295 
  1226 
  1225 
  1227 fun divide_and_conquer decomp x =
  1226 fun divide_and_conquer decomp x =
  1228   let val (ys, recomb) = decomp x
  1227   let val (ys, recomb) = decomp x
  1229   in recomb (map (divide_and_conquer decomp) ys) end;
  1228   in recomb (map (divide_and_conquer decomp) ys) end;
  1230 
  1229 
  1231 (*Partition list into elements that satisfy predicate and those that don't.
       
  1232   Preserves order of elements in both lists.*)
       
  1233 val partition = List.partition;
       
  1234 
       
  1235 fun partition_eq (eq:'a * 'a -> bool) =
       
  1236   let
       
  1237     fun part [] = []
       
  1238       | part (x :: ys) =
       
  1239           let val (xs, xs') = partition (fn y => eq (x, y)) ys
       
  1240           in (x::xs)::(part xs') end
       
  1241   in part end;
       
  1242 
       
  1243 
  1230 
  1244 (*Partition a list into buckets  [ bi, b(i+1), ..., bj ]
  1231 (*Partition a list into buckets  [ bi, b(i+1), ..., bj ]
  1245    putting x in bk if p(k)(x) holds.  Preserve order of elements if possible.*)
  1232    putting x in bk if p(k)(x) holds.  Preserve order of elements if possible.*)
  1246 fun partition_list p i j =
  1233 fun partition_list p i j =
  1247   let fun part k xs =
  1234   let fun part k xs =
  1248             if k>j then
  1235             if k>j then
  1249               (case xs of [] => []
  1236               (case xs of [] => []
  1250                          | _ => raise Fail "partition_list")
  1237                          | _ => raise Fail "partition_list")
  1251             else
  1238             else
  1252             let val (ns, rest) = partition (p k) xs;
  1239             let val (ns, rest) = List.partition (p k) xs;
  1253             in  ns :: part(k+1)rest  end
  1240             in  ns :: part(k+1)rest  end
  1254   in  part i end;
  1241   in  part i end;
       
  1242 
       
  1243 fun partition_eq (eq:'a * 'a -> bool) =
       
  1244   let
       
  1245     fun part [] = []
       
  1246       | part (x :: ys) =
       
  1247           let val (xs, xs') = List.partition (fn y => eq (x, y)) ys
       
  1248           in (x::xs)::(part xs') end
       
  1249   in part end;
       
  1250 
  1255 
  1251 
  1256 
  1252 
  1257 (* generating identifiers *)
  1253 (* generating identifiers *)
  1258 
  1254 
  1259 (** Freshly generated identifiers; supplied prefix MUST start with a letter **)
  1255 (** Freshly generated identifiers; supplied prefix MUST start with a letter **)