Values in Standard ML

Standard ML (SML) has various value types including strings, integers, floats, booleans, etc. Here are a few basic examples.

(* Strings, which can be concatenated with ^ *)
val _ = print("sml" ^ "lang\n")

(* Integers and floats *)
val _ = print("1+1 = " ^ Int.toString(1 + 1) ^ "\n")
val _ = print("7.0/3.0 = " ^ Real.toString(7.0 / 3.0) ^ "\n")

(* Booleans, with boolean operators as you'd expect *)
val _ = print(Bool.toString(true andalso false) ^ "\n")
val _ = print(Bool.toString(true orelse false) ^ "\n")
val _ = print(Bool.toString(not true) ^ "\n")

To run this program, save it in a file (e.g., values.sml) and use an SML interpreter or compiler. For example, if you’re using the Standard ML of New Jersey (SML/NJ) system:

$ sml values.sml
smlang
1+1 = 2
7.0/3.0 = 2.33333333333
false
true
false

In this Standard ML code:

  1. We use ^ for string concatenation.
  2. We use Int.toString and Real.toString to convert numbers to strings for printing.
  3. Boolean operators in SML are andalso, orelse, and not.
  4. We use val _ = ... to execute expressions for their side effects (printing) without binding the result to a name.
  5. SML uses (* ... *) for comments.

Note that SML is a functional language, so this example doesn’t follow the imperative style of the original code. Instead, it demonstrates equivalent operations using SML’s syntax and standard library functions.

查看推荐产品

Comments powered by Disqus