TFL/utils.ML
changeset 10769 70b9b0cfe05f
child 16854 fdd362b7e980
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/TFL/utils.ML	Wed Jan 03 21:20:40 2001 +0100
     1.3 @@ -0,0 +1,78 @@
     1.4 +(*  Title:      TFL/utils.ML
     1.5 +    ID:         $Id$
     1.6 +    Author:     Konrad Slind, Cambridge University Computer Laboratory
     1.7 +    Copyright   1997  University of Cambridge
     1.8 +
     1.9 +Basic utilities.
    1.10 +*)
    1.11 +
    1.12 +signature UTILS =
    1.13 +sig
    1.14 +  exception ERR of {module: string, func: string, mesg: string}
    1.15 +  val C: ('a -> 'b -> 'c) -> 'b -> 'a -> 'c
    1.16 +  val itlist: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
    1.17 +  val rev_itlist: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
    1.18 +  val end_itlist: ('a -> 'a -> 'a) -> 'a list -> 'a
    1.19 +  val itlist2: ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
    1.20 +  val pluck: ('a -> bool) -> 'a list -> 'a * 'a list
    1.21 +  val zip3: 'a list -> 'b list -> 'c list -> ('a*'b*'c) list
    1.22 +  val take: ('a -> 'b) -> int * 'a list -> 'b list
    1.23 +end;
    1.24 +
    1.25 +structure Utils: UTILS =
    1.26 +struct
    1.27 +
    1.28 +(*standard exception for TFL*)
    1.29 +exception ERR of {module: string, func: string, mesg: string};
    1.30 +
    1.31 +fun UTILS_ERR func mesg = ERR {module = "Utils", func = func, mesg = mesg};
    1.32 +
    1.33 +
    1.34 +fun C f x y = f y x
    1.35 +
    1.36 +fun itlist f L base_value =
    1.37 +   let fun it [] = base_value
    1.38 +         | it (a::rst) = f a (it rst)
    1.39 +   in it L
    1.40 +   end;
    1.41 +
    1.42 +fun rev_itlist f =
    1.43 +   let fun rev_it [] base = base
    1.44 +         | rev_it (a::rst) base = rev_it rst (f a base)
    1.45 +   in rev_it
    1.46 +   end;
    1.47 +
    1.48 +fun end_itlist f =
    1.49 +   let fun endit [] = raise UTILS_ERR "end_itlist" "list too short"
    1.50 +         | endit alist =
    1.51 +            let val (base::ralist) = rev alist
    1.52 +            in itlist f (rev ralist) base
    1.53 +            end
    1.54 +   in endit
    1.55 +   end;
    1.56 +
    1.57 +fun itlist2 f L1 L2 base_value =
    1.58 + let fun it ([],[]) = base_value
    1.59 +       | it ((a::rst1),(b::rst2)) = f a b (it (rst1,rst2))
    1.60 +       | it _ = raise UTILS_ERR "itlist2" "different length lists"
    1.61 + in  it (L1,L2)
    1.62 + end;
    1.63 +
    1.64 +fun pluck p  =
    1.65 +  let fun remv ([],_) = raise UTILS_ERR "pluck" "item not found"
    1.66 +        | remv (h::t, A) = if p h then (h, rev A @ t) else remv (t,h::A)
    1.67 +  in fn L => remv(L,[])
    1.68 +  end;
    1.69 +
    1.70 +fun take f =
    1.71 +  let fun grab(0,L) = []
    1.72 +        | grab(n, x::rst) = f x::grab(n-1,rst)
    1.73 +  in grab
    1.74 +  end;
    1.75 +
    1.76 +fun zip3 [][][] = []
    1.77 +  | zip3 (x::l1) (y::l2) (z::l3) = (x,y,z)::zip3 l1 l2 l3
    1.78 +  | zip3 _ _ _ = raise UTILS_ERR "zip3" "different lengths";
    1.79 +
    1.80 +
    1.81 +end;