SML    up

 Hello World   Michael Neumann
print "Hello World\n";
Prints "Hello World" onto the screen.

 Squares (1)   Michael Neumann
fun iter (a,b) =
   if a <= b then (print(Int.toString(a*a) ^ " "); iter(a+1,b) )
   else print "\n";

Outputs the squares from 1 to 10.

 Squares (2)   Michael Neumann
fun iter 0 = ""
|   iter n = (iter(n-1); print(Int.toString(n*n) ^ " "); "\n");

print(iter 10);
Outputs the squares from 1 to 10.

 Word Tokenizer   Michael Neumann
fun wordTokenizer (reader : (char, TextIO.StreamIO.instream) StringCvt.reader)
                  (strm : TextIO.StreamIO.instream) = let

  fun scan s (l: char list) (charState: bool) =
    case (reader s)
      of NONE => (l, s)
       | SOME (c, s) =>
         if Char.isAlpha c then scan s (l @ [c]) true
         else if charState then (l, s)
         else scan s l charState

  val (res, s) = scan strm [] false

  if res = [] then NONE
  else SOME (implode res, s)

fun lowerCaseFilter s =
  case s
    of NONE => NONE
     | SOME w => SOME ( Char.toLower w)

fun main (cmd:string, args:string list):OS.Process.status = let
  val (fileName::_) = args
  val ins = TextIO.openIn fileName

  val ws = lowerCaseFilter o TextIO.scanStream wordTokenizer

  fun loop () = case ws ins
                  of NONE => ()
                   | SOME word => (print word; print "\n"; loop ())
  loop ();

fun test () =
  main ("tokenizer", ["tokenizer.sml"])
Splits a given file into words and outputs them. Makes use of SML streams.

 Hello World   SML/NJ   Michael Neumann
  To make a heap image of this file, load it into the 
  interactive compiler (you do this by starting "sml" at 
  the command line; then issue a 'use "thisfile.sml"'). 
  Type then:

    SMLofNJ.exportFn ("heapimage", main);

  Exit now the interactive compiler. To load the heap image,
  type (at the command line):

    sml @SMLload=/path/to/heapimage.ext

  (where ext is a platform dependend suffix, e.g. "x86-bsd", to
   distinguish heap images of different OSes and platforms)

fun main (cmd:string, args:string list):OS.Process.status =
  print "Hello world\n";
Prints "Hello World" onto the screen.