SML# Document Version 4.0.0
7 Introduction to ML programming

7.19 Principle in composing expressions

The fundamental principle of ML programming to compose expressions, but of course not all compositions yield meaningful programs. In ML, the principle of composing expressions is the following.

expressions are freely composed as far as they are type correct.

Let us consider this principle using lists as examples. List does not constrain its component types; any values can be put into a list as far as their type is the same. The following interactive session shows constructions of lists of various types.

  # fact 4 :: 4 + 4 :: (if factorial 1 = 0 then nil else [1,2,3]);
  val it = [12, 24, 8] : int list
  # [1.1, Math.pi, Math.sqrt 2.0];
  val it = [1.1, 3.14159265359, 1.41421356237] : real list
  # "I"::"became"::"fully"::"operational"::"on"::"April 2, 2012"::nil;
  val it = ["I", "became", "fully", "operational", "on", "April 2, 2012"] : string list
  # [factorial, fib];
  val it = [fn, fn] : (int -> int) list
  # [#"S", #"M", #"L", #"#"];
  val it = [#"S", #"M", #"L", #"#"] : char list
  # implode it;
  val it = "SML#" : string
  # explode it;
  val it = [#"S", #"M", #"L", #"#"] : char list

”implode” convert list of characters into a string and ”explode” does its converse.