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
5fun print s = TextIO.print s
6
7(* NJ93 Integer *)
8
9fun max (x, y) = if x > y then x else y : int;
10fun min (x, y) = if x > y then y else x : int;
11
12(* NJ93 List *)
13
14exception Hd and Tl and Nth and NthTail
15
16fun hd arg      = (List.hd arg) handle Empty => raise Hd;
17fun tl arg      = (List.tl arg) handle Empty => raise Tl;
18fun nthtail arg = (List.drop arg) handle Subscript => raise NthTail;
19fun nth arg     = (List.nth arg) handle Subscript => raise Nth;
20
21fun app f xs =
22    let fun h []      = ()
23	  | h (x::xr) = (f x; h xr)
24    in h xs end;
25fun revapp f xs =
26    let fun h []      = ()
27	  | h (x::xr) = (h xr; f x; ())
28    in h xs end;
29
30fun fold f xs e    = List.foldr f e xs;
31fun revfold f xs e = List.foldl f e xs;
32
33(* NJ93 Real *)
34
35fun ceiling r  = ceil r
36fun truncate r = trunc r
37
38(* NJ93 Ref *)
39
40fun inc r = r := !r+1;
41fun dec r = r := !r-1;
42
43(* NJ93 String *)
44
45prim_val chr : int    -> string = 1 "sml_chr";
46prim_val ord : string -> int    = 1 "sml_ord";
47
48local
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";
54in
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;
84end;
85
86(* NJ93 top-level math functions *)
87
88prim_val sqrt   : real -> real = 1 "sml_sqrt";
89prim_val sin    : real -> real = 1 "sml_sin";
90prim_val cos    : real -> real = 1 "sml_cos";
91prim_val arctan : real -> real = 1 "atan_float";
92prim_val exp    : real -> real = 1 "sml_exp";
93prim_val ln     : real -> real = 1 "sml_ln";
94
95(* NJ93 top-level input/output *)
96
97open BasicIO
98
99