home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-08-18 | 2.3 KB | 104 lines | [TEXT/R*ch] |
- (* Fnlib.sml. Library functions *)
-
- exception Impossible of string;
-
- fun fatalError s = raise(Impossible s);
-
- fun getOption NONE = fatalError "getOption"
- | getOption (SOME a) = a
- ;
-
- fun fst (x, y) = x;
- fun snd (x, y) = y;
-
- fun incr r = r := !r + 1;
- fun decr r = r := !r - 1;
-
- fun mapFrom f n [] = []
- | mapFrom f n (x :: xs) = f n x :: mapFrom f (n+1) xs
- ;
-
- fun map2 f [] [] = []
- | map2 f (x :: xs) (y :: ys) = f x y :: map2 f xs ys
- | map2 f _ _ = fatalError "map2: lists of different length"
- ;
-
- fun appFrom f n [] = ()
- | appFrom f n (x :: xs) = (f n x : unit; appFrom f (n+1) xs)
- ;
-
- fun app2 f [] [] = ()
- | app2 f (x::xs) (y::ys) = (f x y : unit; app2 f xs ys)
- | app2 f _ _ = fatalError "app2: lists of different length"
- ;
-
- fun foldL f a [] = a
- | foldL f a (x::xs) = foldL f (f x a) xs
- ;
-
- fun foldL_zip f a [] [] = a
- | foldL_zip f a (x::xs) (y::ys) = foldL_zip f (f x y a) xs ys
- | foldL_zip f a _ _ = fatalError "foldL_zip: lists of different length"
- ;
-
- fun foldL_map f g a [] = a
- | foldL_map f g a (x::xs) = foldL_map f g (f (g x) a) xs
- ;
-
- fun foldR f a [] = a
- | foldR f a (x::xs) = f x (foldR f a xs)
- ;
-
- fun foldR1 f [] = fatalError "foldR1: an empty argument"
- | foldR1 f [x] = x
- | foldR1 f (x::xs) = f x (foldR1 f xs)
- ;
-
- fun foldR_map f g e [] = e
- | foldR_map f g e (x::xs) = f (g x) (foldR_map f g e xs)
- ;
-
- fun map_fields f [] = []
- | map_fields f ((lab, t) :: xs) = (lab, f t) :: map_fields f xs
- ;
-
- fun all_fields f [] = true
- | all_fields f ((_, t) :: xs) =
- (f t) andalso all_fields f xs
- ;
-
- fun exists_field f [] = false
- | exists_field f ((_, t) :: xs) =
- (f t) orelse exists_field f xs
- ;
-
- fun app_field f [] = ()
- | app_field f ((_, t) :: xs) = (f t : unit; app_field f xs)
- ;
-
- fun member k [] = false
- | member k (x :: xs) =
- if k = x then true else member k xs
- ;
-
- fun lookup k [] = raise Subscript
- | lookup k ((a, v) :: xs) =
- if k = a then v else lookup k xs
- ;
-
- fun duplicates [] = false
- | duplicates (x :: xs) = member x xs orelse duplicates xs
- ;
- fun stringToLower s =
- CharVector.tabulate(size s, fn i => Char.toLower(CharVector.sub(s, i)));
-
- fun for f i j =
- if i > j then () else (f i : unit; for f (i+1) j)
- ;
-
- fun zip2 [] [] = []
- | zip2 [] (y :: ys) = fatalError "zip2"
- | zip2 (x :: xs) [] = fatalError "zip2"
- | zip2 (x :: xs) (y :: ys) = (x, y) :: zip2 xs ys
- ;
-