home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / mosmllib / NJ93.sml < prev    next >
Encoding:
Text File  |  1997-08-18  |  2.7 KB  |  99 lines  |  [TEXT/R*ch]

  1. (* NJ93.sml 1995-02-24, 1995-11-22, 1996-07-12
  2.    Half-way compatibility with those SML/NJ 0.93 basis structures
  3.    which were open in the initial environment. *)
  4.  
  5. fun print s = TextIO.print s
  6.  
  7. (* NJ93 Integer *)
  8.  
  9. fun max (x, y) = if x > y then x else y : int;
  10. fun min (x, y) = if x > y then y else x : int;
  11.  
  12. (* NJ93 List *)
  13.  
  14. exception Hd and Tl and Nth and NthTail
  15.  
  16. fun hd arg      = (List.hd arg) handle Empty => raise Hd;
  17. fun tl arg      = (List.tl arg) handle Empty => raise Tl;
  18. fun nthtail arg = (List.drop arg) handle Subscript => raise NthTail;
  19. fun nth arg     = (List.nth arg) handle Subscript => raise Nth;
  20.  
  21. fun app f xs =
  22.     let fun h []      = ()
  23.       | h (x::xr) = (f x; h xr)
  24.     in h xs end;
  25. fun revapp f xs =
  26.     let fun h []      = ()
  27.       | h (x::xr) = (h xr; f x; ())
  28.     in h xs end;
  29.  
  30. fun fold f xs e    = List.foldr f e xs;
  31. fun revfold f xs e = List.foldl f e xs;
  32.  
  33. (* NJ93 Real *)
  34.  
  35. fun ceiling r  = ceil r
  36. fun truncate r = trunc r
  37.  
  38. (* NJ93 Ref *)
  39.  
  40. fun inc r = r := !r+1;
  41. fun dec r = r := !r-1;
  42.  
  43. (* NJ93 String *)
  44.  
  45. prim_val chr : int    -> string = 1 "sml_chr";
  46. prim_val ord : string -> int    = 1 "sml_ord";
  47.  
  48. local 
  49.     prim_val create_string_ : int -> string                = 1 "create_string";
  50.     prim_val nth_char_      : string -> int -> int         = 2 "get_nth_char";
  51.     prim_val set_nth_char_  : string -> int -> int -> unit = 3 "set_nth_char";
  52.     prim_val blit_string_   : string -> int -> string -> int -> int -> unit 
  53.                                                            = 5 "blit_string";
  54. in 
  55.     exception Substring;
  56.     fun ordof(s, i) = 
  57.     if i < 0 orelse i >= size s then raise Ord
  58.     else nth_char_ s i;
  59.     fun substring (s, i, n) = (String.substring (s, i, n)) 
  60.                           handle Subscript => raise Substring;
  61.     fun explode s =
  62.     let fun loop 0 acc = acc
  63.           | loop n acc =
  64.         let val n' = n - 1
  65.             val x = create_string_ 1
  66.         in
  67.             set_nth_char_ x 0 (nth_char_ s n');
  68.             loop n' (x::acc)
  69.         end
  70.     in loop (size s) [] end;
  71.  
  72.     fun implode ss =
  73.     let fun resultSizeAcc [] acc = acc
  74.           | resultSizeAcc (s::ss) acc = resultSizeAcc ss (acc + size s)
  75.         val rlen = resultSizeAcc ss 0
  76.         val r = create_string_ rlen
  77.         fun loop [] i = ()
  78.           | loop (s::ss) i =
  79.         let val slen = size s in
  80.             blit_string_ s 0 r i slen;
  81.             loop ss (i + slen)
  82.         end
  83.     in loop ss 0; r end;
  84. end;
  85.  
  86. (* NJ93 top-level math functions *)
  87.  
  88. prim_val sqrt   : real -> real = 1 "sml_sqrt";
  89. prim_val sin    : real -> real = 1 "sml_sin";
  90. prim_val cos    : real -> real = 1 "sml_cos";
  91. prim_val arctan : real -> real = 1 "atan_float";
  92. prim_val exp    : real -> real = 1 "sml_exp";
  93. prim_val ln     : real -> real = 1 "sml_ln";
  94.  
  95. (* NJ93 top-level input/output *)
  96.  
  97. open BasicIO
  98.  
  99.