src/Pure/library.ML
changeset 33955 fff6f11b1f09
parent 33206 fee21bb23d22
child 34059 f3f0e20923a7
equal deleted inserted replaced
33954:1bc3b688548c 33955:fff6f11b1f09
    79   val fold_burrow: ('a list -> 'c -> 'b list * 'd) -> 'a list list -> 'c -> 'b list list * 'd
    79   val fold_burrow: ('a list -> 'c -> 'b list * 'd) -> 'a list list -> 'c -> 'b list list * 'd
    80   val maps: ('a -> 'b list) -> 'a list -> 'b list
    80   val maps: ('a -> 'b list) -> 'a list -> 'b list
    81   val filter: ('a -> bool) -> 'a list -> 'a list
    81   val filter: ('a -> bool) -> 'a list -> 'a list
    82   val filter_out: ('a -> bool) -> 'a list -> 'a list
    82   val filter_out: ('a -> bool) -> 'a list -> 'a list
    83   val map_filter: ('a -> 'b option) -> 'a list -> 'b list
    83   val map_filter: ('a -> 'b option) -> 'a list -> 'b list
       
    84   val take: int -> 'a list -> 'a list
       
    85   val drop: int -> 'a list -> 'a list
    84   val chop: int -> 'a list -> 'a list * 'a list
    86   val chop: int -> 'a list -> 'a list * 'a list
    85   val dropwhile: ('a -> bool) -> 'a list -> 'a list
    87   val dropwhile: ('a -> bool) -> 'a list -> 'a list
    86   val nth: 'a list -> int -> 'a
    88   val nth: 'a list -> int -> 'a
    87   val nth_map: int -> ('a -> 'a) -> 'a list -> 'a list
    89   val nth_map: int -> ('a -> 'a) -> 'a list -> 'a list
    88   val nth_drop: int -> 'a list -> 'a list
    90   val nth_drop: int -> 'a list -> 'a list
   222 sig
   224 sig
   223   include BASIC_LIBRARY
   225   include BASIC_LIBRARY
   224   val foldl: ('a * 'b -> 'a) -> 'a * 'b list -> 'a
   226   val foldl: ('a * 'b -> 'a) -> 'a * 'b list -> 'a
   225   val foldr: ('a * 'b -> 'b) -> 'a list * 'b -> 'b
   227   val foldr: ('a * 'b -> 'b) -> 'a list * 'b -> 'b
   226   val foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list
   228   val foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list
   227   val take: int * 'a list -> 'a list
       
   228   val drop: int * 'a list -> 'a list
       
   229   val last_elem: 'a list -> 'a
   229   val last_elem: 'a list -> 'a
   230 end;
   230 end;
   231 
   231 
   232 structure Library: LIBRARY =
   232 structure Library: LIBRARY =
   233 struct
   233 struct
   421 
   421 
   422 val filter = List.filter;
   422 val filter = List.filter;
   423 fun filter_out f = filter (not o f);
   423 fun filter_out f = filter (not o f);
   424 val map_filter = List.mapPartial;
   424 val map_filter = List.mapPartial;
   425 
   425 
       
   426 fun take (0: int) xs = []
       
   427   | take _ [] = []
       
   428   | take n (x :: xs) =
       
   429       if n > 0 then x :: take (n - 1) xs else [];
       
   430 
       
   431 fun drop (0: int) xs = xs
       
   432   | drop _ [] = []
       
   433   | drop n (x :: xs) =
       
   434       if n > 0 then drop (n - 1) xs else x :: xs;
       
   435 
   426 fun chop (0: int) xs = ([], xs)
   436 fun chop (0: int) xs = ([], xs)
   427   | chop _ [] = ([], [])
   437   | chop _ [] = ([], [])
   428   | chop n (x :: xs) = chop (n - 1) xs |>> cons x;
   438   | chop n (x :: xs) = chop (n - 1) xs |>> cons x;
   429 
       
   430 (*take the first n elements from a list*)
       
   431 fun take (n: int, []) = []
       
   432   | take (n, x :: xs) =
       
   433       if n > 0 then x :: take (n - 1, xs) else [];
       
   434 
       
   435 (*drop the first n elements from a list*)
       
   436 fun drop (n: int, []) = []
       
   437   | drop (n, x :: xs) =
       
   438       if n > 0 then drop (n - 1, xs) else x :: xs;
       
   439 
   439 
   440 fun dropwhile P [] = []
   440 fun dropwhile P [] = []
   441   | dropwhile P (ys as x::xs) = if P x then dropwhile P xs else ys;
   441   | dropwhile P (ys as x::xs) = if P x then dropwhile P xs else ys;
   442 
   442 
   443 (*return nth element of a list, where 0 designates the first element;
   443 (*return nth element of a list, where 0 designates the first element;