Skip to content

Latest commit

 

History

History

ullman-examples

Code examples from Ullman's Book

Standard ML Directory ullman-examples\ contains Standard ML code examples from Ullman's book "Elements of ML Programming" (2nd Edition, 1997).
It also includes several build scripts (batch files) for experimenting with Standard ML on a Windows machine.

3_mergsort Example

This example has the following directory structure :

> tree /a /f . | findstr /v /b [A-Z]
|   build.bat
|   build.cm
\---src
        main.sml

We execute the slightly modified SML script file target\main.sml as follows :

> build clean run
Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024]
[opening K:\ullman-examples\3_mergeSort\target\main.sml]
val merge = fn : int list * int list -> int list
val split = fn : 'a list -> 'a list * 'a list
val mergeSort = fn : int list -> int list
val listToString = fn : int list -> string
list=1,3,7,2,9,6
sorted=1,2,3,6,7,9

Note: build.bat does simply appends a new line to the executed script in order to force the evaluation of the main function (together with the appropriate arguments).

> diff src\main.sml target\mail.sml
12a13,14
>
> val _ = MergeSort.main []

We further specify compile in order to generate and execute the SML image target\3_mergeSort-image.x86-win32 :

> build clean compile run
Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024]
list=1,3,7,2,9,6
sorted=1,2,3,6,7,9

3_komult Example

This example has the following directory structure :

> tree /a /f . | findstr /v /b [A-Z]
|   build.bat
|   build.cm
\---src
        main.sml

We execute the slightly modified SML script file target\main.sml as follows :

> build clean run
Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024]
[opening K:\ullman-examples\3_komult\target\main.sml]
val padd = fn : real list * real list -> real list
val smult = fn : real list * real -> real list
val pmult = fn : real list * real list -> real list
val psub = fn : real list * real list -> real list
val length = fn : 'a list -> int
val bestSplit = fn : int * int -> int
val shift = fn : real list * int -> real list
val carve = fn : 'a list * int -> 'a list * 'a list
val komult = fn : real list * real list -> real list
val list_string = fn : real list -> string
p=1,4
q=2,5
komult=2,13,20

We further specify compile in order to generate and execute the SML image target\3_komult-image.x86-win32 :

> build clean compile run
Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024]
p=1,4
q=2,5
komult=2,13,20

4_comb Example

This example has the following directory structure :

> tree /a /f . | findstr /v /b [A-Z]
|   build.bat
|   build.cm
\---src
        main.sml

We execute the slightly modified SML script file target\main.sml as follows :

> build clean run
Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024]
[opening K:\ullman-examples\4_comb\target\main.sml]
opening TextIO
  type vector = string
  [...]
  structure StreamIO :
    sig
      type vector = string
      [...]
      val outputSubstr : outstream * substring -> unit
    end
  [...]
XXXXX
XXXX
XXX
XX
XX
X
X
XXX
XX
X
X
XX
XXXX
XXX
XX
X
X
XX
XXX
comb(5, 2)=10

4_sumInts Example

This example demonstrates the usage of the TextIO standard module; it takes as input the text file sumInts.txt and prints out the sum.

This example has the following directory structure :

> tree /a /f . | findstr /v /b [A-Z]
|   build.bat
|   build.cm
|   sumInts.txt
\---src
        main.sml

We execute the slightly modified SML script file target\main.sml as follows :

> build clean run
Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024]
[opening K:\ullman-examples\4_sumInts\target\main.sml]
opening TextIO
  type vector = string
  [...]
  structure StreamIO :
    sig
      type vector = string
      [...]
      val outputSubstr : outstream * substring -> unit
    end
  [...]
val END = ~1 : int
val digit = fn : char -> bool
val startInt = fn : instream -> int
val startInt1 = fn : instream * elem option -> int
val finishInt = fn : int * instream -> int
val finishInt1 = fn : int * instream * elem option -> int
val getInt = fn : instream -> int
val sumInts1 = fn : instream -> int
val sumInts = fn : string -> int
sumInts=61

5_parseExpression Example

This example has the following directory structure :

> tree /a /f . | findstr /v /b [A-Z]
|   00download.txt
|   build.bat
|   build.cm
|   parseExpression.txt
\---src
        main.sml

We execute the slightly modified SML script file target\main.sml as follows :

> build clean run
Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024]
[opening K:\ullman-examples\5_parseExpression\target\main.sml]
opening TextIO
  type vector = string
  [...]
  structure StreamIO :
    sig
      type vector = string
      [...]
      val outputSubstr : outstream * substring -> unit
    end
  [...]
val digit = fn : char -> bool
val blank = fn : char -> bool
val lookahead2 = fn : instream -> char option
val integer = fn : instream * int -> int
val atom = fn : instream -> int
val term = fn : instream -> int
val termTail = fn : instream * int -> int
val expression = fn : instream -> int
val expTail = fn : instream * int -> int
val infile = - : instream
expression=9

We further specify compile in order to generate and execute the SML image target\5_parseExpression-image.x86-win32 :

> build clean compile run
Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024]
expression=9

6_sumElList Example

This example has the following directory structure :

> tree /a /f . | findstr /v /b [A-Z]
|   build.bat
|   build.cm
\---src
        main.sml

We execute the slightly modified SML script file target\main.sml as follows :

> build clean run
Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024]
[opening K:\ullman-examples\6_sumElList\target\main.sml]
datatype ('a,'b) element = P of 'a * 'b | S of 'a
val sumElList = fn : ('a,int) element list -> int
sumElList=12

We further specify compile in order to generate and execute the SML image target\6_sumElList-image.x86-win32 :

> build clean compile run
Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024]
lst=P(1,4),S(5),P(2,5),P(9,3),
sumElList=12

8_BTree Example

This example has the following directory structure :

> tree /a /f . | findstr /v /b [A-Z]
|   build.bat
|   build.cm
\---src
        main.sml

We execute the slightly modified SML script file target\main.sml as follows :

> build -verbose clean run
Execute SML script "target\main.sml" (args: )
Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024]
[opening K:\ullman-examples\8_BTree\target\main.sml]
[autoloading]
[library $SMLNJ-BASIS/basis.cm is stable]
[library $SMLNJ-BASIS/(basis.cm):basis-common.cm is stable]
[autoloading done]
tree=a,t,x

We further specify compile in order to generate and execute the SML image target\8_BTree-image.x86-win32 :

> build -verbose clean compile run
Compile SML source file "main"
Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024]
[scanning ../build.cm]
[parsing ../(build.cm):src/main.sml]
[creating directory ..\src\.cm\SKEL]
[library $SMLNJ-BASIS/basis.cm is stable]
[library $SMLNJ-BASIS/(basis.cm):basis-common.cm is stable]
[compiling ../(build.cm):src/main.sml]
[creating directory ..\src\.cm\GUID]
[creating directory ..\src\.cm\x86-win32]
[code: 6275, data: 171, env: 601 bytes]
[scanning XYZ_XXX_cmfile.cm]
[scanning (XYZ_XXX_cmfile.cm):../build.cm]
[parsing (XYZ_XXX_cmfile.cm):XYZ_XXX_smlfile.sml]
[creating directory .cm\SKEL]
[compiling (XYZ_XXX_cmfile.cm):XYZ_XXX_smlfile.sml]
[creating directory .cm\GUID]
[creating directory .cm\x86-win32]
[code: 529, data: 77, env: 39 bytes]
Execute SML image "target\8_BTree-image.x86-win32" (args: )
tree=a,t,x

8_Structs Example

This example has the following directory structure :

> tree /a /f . | findstr /v /b [A-Z]
|   build.bat
|   build.cm
\---src
        main.sml

We execute the slightly modified SML script file target\main.sml as follows :

> build -verbose clean run
Execute SML script "target\main.sml" (args: )
Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024]
[opening K:\ullman-examples\8_Structs\target\main.sml]
signature SIG1 =
  sig
  val i : int
  type t
  val x : t
  val y : t
  val f : t * t -> bool
  val T_toString : t -> string
end
[autoloading]
[library $SMLNJ-BASIS/basis.cm is stable]
[library $SMLNJ-BASIS/(basis.cm):basis-common.cm is stable]
[autoloading done]
K:\ullman-examples\8_Structs\target\main.sml:17.22 Warning: calling polyEqual
structure Struct1 : sig
  val i : int
  type t = int
  val x : int
  val y : int
  val f : ''a * ''a -> bool
  val T_toString : int -> string
end
structure Struct2 : SIG1
structure Struct3 : SIG1
[autoloading]
[autoloading done]
i=3, x=4, y=5
b1=false
b2=false
b3=true

i=3, x=4, y=5
b1=false

We further specify compile in order to generate and execute the SML image target\8_Structs-image.x86-win32 :

> build -verbose clean compile run
Compile SML source file "main"
Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024]
[scanning ../build.cm]
[parsing ../(build.cm):src/main.sml]
[creating directory ..\src\.cm\SKEL]
[library $SMLNJ-BASIS/basis.cm is stable]
[library $SMLNJ-BASIS/(basis.cm):basis-common.cm is stable]
[compiling ../(build.cm):src/main.sml]
[...]
Execute SML image "target\8_Structs-image.x86-win32" (args: )
i=3, x=4, y=5
b1=false
b2=false
b3=true

i=3, x=4, y=5
b1=false

mics/March 2025