src/Pure/library.ML
changeset 25538 58e8ba3b792b
parent 25224 6d4d26e878f4
child 25549 7040555f20c7
equal deleted inserted replaced
25537:55017c8b0a24 25538:58e8ba3b792b
    98   val get_index: ('a -> 'b option) -> 'a list -> (int * 'b) option
    98   val get_index: ('a -> 'b option) -> 'a list -> (int * 'b) option
    99   val get_first: ('a -> 'b option) -> 'a list -> 'b option
    99   val get_first: ('a -> 'b option) -> 'a list -> 'b option
   100   val eq_list: ('a * 'b -> bool) -> 'a list * 'b list -> bool
   100   val eq_list: ('a * 'b -> bool) -> 'a list * 'b list -> bool
   101   val map2: ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
   101   val map2: ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
   102   val fold2: ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
   102   val fold2: ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
       
   103   val map_product: ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
       
   104   val fold_product: ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
   103   val zip_options: 'a list -> 'b option list -> ('a * 'b) list
   105   val zip_options: 'a list -> 'b option list -> ('a * 'b) list
   104   val ~~ : 'a list * 'b list -> ('a * 'b) list
   106   val ~~ : 'a list * 'b list -> ('a * 'b) list
   105   val split_list: ('a * 'b) list -> 'a list * 'b list
   107   val split_list: ('a * 'b) list -> 'a list * 'b list
   106   val separate: 'a -> 'a list -> 'a list
   108   val separate: 'a -> 'a list -> 'a list
   107   val replicate: int -> 'a -> 'a list
   109   val replicate: int -> 'a -> 'a list
   108   val multiply: 'a list -> 'a list list -> 'a list list
   110   val multiply: 'a list -> 'a list list -> 'a list list
   109   val product: 'a list -> 'b list -> ('a * 'b) list
       
   110   val filter: ('a -> bool) -> 'a list -> 'a list
   111   val filter: ('a -> bool) -> 'a list -> 'a list
   111   val filter_out: ('a -> bool) -> 'a list -> 'a list
   112   val filter_out: ('a -> bool) -> 'a list -> 'a list
   112   val map_filter: ('a -> 'b option) -> 'a list -> 'b list
   113   val map_filter: ('a -> 'b option) -> 'a list -> 'b list
   113   val is_prefix: ('a * 'a -> bool) -> 'a list -> 'a list -> bool
   114   val is_prefix: ('a * 'a -> bool) -> 'a list -> 'a list -> bool
   114   val take_prefix: ('a -> bool) -> 'a list -> 'a list * 'a list
   115   val take_prefix: ('a -> bool) -> 'a list -> 'a list * 'a list
   427   in length list1 = length list2 andalso eq_lst (list1, list2) end;
   428   in length list1 = length list2 andalso eq_lst (list1, list2) end;
   428 
   429 
   429 fun maps f [] = []
   430 fun maps f [] = []
   430   | maps f (x :: xs) = f x @ maps f xs;
   431   | maps f (x :: xs) = f x @ maps f xs;
   431 
   432 
       
   433 (*copy the list preserving elements that satisfy the predicate*)
       
   434 val filter = List.filter;
       
   435 fun filter_out f = filter (not o f);
       
   436 val map_filter = List.mapPartial;
       
   437 
   432 fun chop (0: int) xs = ([], xs)
   438 fun chop (0: int) xs = ([], xs)
   433   | chop _ [] = ([], [])
   439   | chop _ [] = ([], [])
   434   | chop n (x :: xs) = chop (n - 1) xs |>> cons x;
   440   | chop n (x :: xs) = chop (n - 1) xs |>> cons x;
   435 
   441 
   436 (*take the first n elements from a list*)
   442 (*take the first n elements from a list*)
   538 (*multiply [a, b, c, ...] * [xs, ys, zs, ...]*)
   544 (*multiply [a, b, c, ...] * [xs, ys, zs, ...]*)
   539 fun multiply [] _ = []
   545 fun multiply [] _ = []
   540   | multiply (x :: xs) yss = map (cons x) yss @ multiply xs yss;
   546   | multiply (x :: xs) yss = map (cons x) yss @ multiply xs yss;
   541 
   547 
   542 (*direct product*)
   548 (*direct product*)
   543 fun product _ [] = []
   549 fun map_product f _ [] = []
   544   | product [] _ = []
   550   | map_product f [] _ = []
   545   | product (x :: xs) ys = map (pair x) ys @ product xs ys;
   551   | map_product f (x :: xs) ys = map (f x) ys @ map_product f xs ys;
   546 
   552 
   547 
   553 fun fold_product f _ [] z = z
   548 (* filter *)
   554   | fold_product f [] _ z = z
   549 
   555   | fold_product f (x :: xs) ys z = z |> fold (f x) ys |> fold_product f xs ys;
   550 (*copy the list preserving elements that satisfy the predicate*)
   556 
   551 val filter = List.filter;
   557 (*lists of pairs*)
   552 fun filter_out f = filter (not o f);
       
   553 val map_filter = List.mapPartial;
       
   554 
       
   555 
       
   556 (* lists of pairs *)
       
   557 
   558 
   558 exception UnequalLengths;
   559 exception UnequalLengths;
   559 
   560 
   560 fun map2 _ [] [] = []
   561 fun map2 _ [] [] = []
   561   | map2 f (x :: xs) (y :: ys) = f x y :: map2 f xs ys
   562   | map2 f (x :: xs) (y :: ys) = f x y :: map2 f xs ys