Standard ML Examples ⬆
This repository gathers Standard ML code examples from various websites. It also includes several build scripts (batch files, Make scripts) for experimenting with Standard ML on a Windows machine. |
Standard ML code can be executed either as a script or as a module.
SML | SML/NJ commands | Moscow ML commands |
---|---|---|
script | sml <script-file> |
mosml <script-file> |
module a) |
ml-build <cm-file>b)<module-main> <image-name> |
mosmlc <source-file> |
b) Compilation manager file (similar in the spirit to a Makefile).
Futhermore there are also two ways to execute each of the following code examples as an SML script :
-
either interactively by running the REPL and writing
use "src/hello.sml"
(below the '-
' character is the SML prompt) :> %SMLNJ_HOME%\bin\sml.bat Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024] - use "src/hello.sml"; [opening src/hello.sml] [autoloading] [library $SMLNJ-BASIS/basis.cm is stable] [library $SMLNJ-BASIS/(basis.cm):basis-common.cm is stable] [autoloading done] structure Hello : sig val hello : unit -> unit val main : 'a -> 'b end val it = () : unit - Hello.main(); stdIn:2.1-2.13 Warning: type vars not generalized because of value restriction are instantiated to dummy types (X1,X2,...) Hello World
-
or as a slightly modified SML script file :
- SML/NJ (option
-smlnj
, default)> build -verbose clean run Execute SML script "target\hello-sml" Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024] [opening K:\examples\hello\target\hello.sml] [autoloading] [library $SMLNJ-BASIS/basis.cm is stable] [library $SMLNJ-BASIS/(basis.cm):basis-common.cm is stable] [autoloading done] Hello World
- Moscow ML (option
-mosml
)> build -verbose -mosml clean run Execute SML script "target\hello.sml" 1 a Moscow ML version 2.01 (January 2004) Enter `quit();' to quit. [opening file "K:\examples\hello\target\hello.sml"] > val it = () : unit > val it = () : unit > val it = () : unit Hello World
Note:
build.bat
does simply appends a new line to the executed script in order to force the evaluation of themain
function (together with the appropriate arguments).> diff src\hello.sml target\hello.sml 12a13,14 > > val _ = Hello.main ["1","a"]
- SML/NJ (option
This project has the following directory structure :
> tree /a /f .| findstr /v /b [A-Z] | build.bat | build.cm \---src args.sml
We execute the slightly modified SML script file target\args.sml
as follows :
> build -verbose clean run Execute SML script "args" Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024] [opening K:\examples\args\target\args.sml] [autoloading] [library $SMLNJ-BASIS/basis.cm is stable] [library $SMLNJ-BASIS/(basis.cm):basis-common.cm is stable] [autoloading done] Command: C:\opt\smlnj\bin\.run\run.x86-win32.exe Arguments: 1 That's right! 1
We add compile
to the above command in order to generate and execute the SML image args-image.x86-win32
:
> build -verbose clean compile run Compile SML source file "args" Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024] [scanning ../build.cm] [parsing ../(build.cm):src/args.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/args.sml] [...] [code: 334, data: 74, env: 39 bytes] Execute SML image "target\args-image.x86-win32" That's right! 1
Note: The output directory contains the generated SML image file with the suffix
x86-win32
, a combinaison ofx86
for a particular architecture andwin32
for the operating system.> tree /a /f target | findstr /v /b [A-Z] | args-image.x86-win32 (240 KB) \---.cm +---GUID +---SKEL \---x86-win32
hello
Example ▴
This project has the following directory structure :
> tree /a /f .| findstr /v /b [A-Z] | build.bat | build.cm \---src hello.sml
We execute the slightly modified SML script file [tatget\hello.sml
] :
> build -verbose clean run Execute SML script "hello" Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024] [opening K:\examples\hello\target\hello.sml] [autoloading] [library $SMLNJ-BASIS/basis.cm is stable] [library $SMLNJ-BASIS/(basis.cm):basis-common.cm is stable] [autoloading done] Hello World
We add compile
to the above command in order to generate and execute the SML image hello-image.x86-win32
:
> build -verbose clean compile run Compile SML source file "hello" Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024] [scanning ../build.cm] [parsing ../(build.cm):src/hello.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/hello.sml] [...] [code: 335, data: 75, env: 39 bytes] Execute SML image "target\hello-image.x86-win32" Hello World
norm
Example ▴
This project has the following directory structure :
> tree /a /f . | findstr /v /b [A-Z] | build.bat | build.cm \---src helper.sml main.sml
We execute the SML script file target\main.sml
as follows :
> build -verbose clean run Execute SML script "main" Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024] [opening K:\examples\norm\target\main.sml] [opening src/script/helper.sml] [autoloading] [library $SMLNJ-BASIS/basis.cm is stable] [library $SMLNJ-BASIS/(basis.cm):basis-common.cm is stable] [autoloading done] val intSquare = fn : int -> int val square = fn : real -> real val cube = fn : real -> real val pi = 3.1415926535 : real val degreeToRadian = fn : real -> real val it = () : unit [autoloading] [autoloading done] 31.0062766776 1.57079632675 3.60555127546
This code example is taken from the Williams College course "CSCI 334 – Principles of Programming Languages"; it has the following directory structure :
> tree /a /f .| findstr /v /b [A-Z] | 00download.txt | build.bat | build.cm \---src qsort.sml
We execute the slightly modified SML script file target\qsort.sml
as follows :
-
SML/NJ (option
-smlnj
, default)> build -verbose clean run Execute SML script "target\qsort.sml" Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024] [opening K:\examples\qsort\target\qsort.sml] [autoloading] [library $smlnj/compiler/current.cm is stable] [...] [autoloading done] val it = () : unit val it = () : unit [autoloading] [autoloading done] list=6,23,6,3,78,23,12,6,7,34,7,1,23,7,23,6,3,6 qsort=1,3,3,6,6,6,6,6,7,7,7,12,23,23,23,23,34,78
-
Moscow ML (option
-mosml
)> build -verbose -mosml clean run Execute SML script "target\qsort.sml" Moscow ML version 2.01 (January 2004) Enter `quit();' to quit. [opening file "K:\examples\qsort\target\qsort.sml"] > val it = () : unit > val it = () : unit > val it = () : unit > val it = () : unit list=6,23,6,3,78,23,12,6,7,34,7,1,23,7,23,6,3,6 qsort=1,3,3,6,6,6,6,6,7,7,7,12,23,23,23,23,34,78
This code example is taken from the Williams College course "CSCI 334 – Principles of Programming Languages"; it has the following directory structure :
> tree /a /f .| findstr /v /b [A-Z] | 00download.txt | build.bat | build.cm \---src polysort.sml
We execute the slightly modified SML script file target\polysort.sml
as follows :
-
SML/NJ (option
-smlnj
, default)> build -verbose clean run Execute SML script "target\polysort.sml" Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024] [opening K:\examples\polysort\target\polysort.sml] [autoloading] [library $smlnj/compiler/current.cm is stable] [...] [autoloading done] val it = () : unit val it = () : unit val partition = fn : ('a * 'b -> bool) * 'b * 'a list -> 'a list * 'a list val qsort = fn : ('a * 'a -> bool) * 'a list -> 'a list [autoloading] [autoloading done] list1=6,23,6,3,78,23,12,6,7,34,7 qsort1=3,6,6,6,7,7,12,23,23,34,78 list2=~3,5,2,~1 qsort2=~1,2,~3,5 list3=moo,purple,cow,wombat qsort3=cow,moo,purple,wombat
-
Moscow ML (option
-mosml
)> build -verbose -mosml clean run Execute SML script "target\polysort.sml" Moscow ML version 2.01 (January 2004) Enter `quit();' to quit. [opening file "K:\examples\polysort\target\polysort.sml"] > val it = () : unit > val it = () : unit > val it = () : unit > val it = () : unit > val ('a, 'b) partition = fn : ('a * 'b -> bool) * 'b * 'a list -> 'a list * 'a list > val 'a qsort = fn : ('a * 'a -> bool) * 'a list -> 'a list list1=6,23,6,3,78,23,12,6,7,34,7 qsort1=3,6,6,6,7,7,12,23,23,34,78 list2=~3,5,2,~1 qsort2=~1,2,~3,5 list3=moo,purple,cow,wombat qsort3=cow,moo,purple,wombat
This project has the following directory structure :
> tree /a /f .| findstr /v /b [A-Z] | build.bat | build.cm \---src sumpairs.sml
We execute the slightly modified SML script file target\sumpairs.sml
as follows :
-
SML/NJ (option
-smlnj
, default)> build -verbose clean run Execute SML script "sumpairs" Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024] [opening K:\examples\sumpairs\target\sumpairs.sml] [autoloading] [library $SMLNJ-BASIS/basis.cm is stable] [library $SMLNJ-BASIS/(basis.cm):basis-common.cm is stable] [autoloading done] val sumpairs = fn : int list -> int list val listToString = fn : int list -> string list=1,2,3,4 sumpairs=3,7
-
Moscow ML (option
-mosml
)> build -verbose -mosml clean run Execute SML script "target\sumpairs.sml" Moscow ML version 2.01 (January 2004) Enter `quit();' to quit. [opening file "K:\examples\sumpairs\target\sumpairs.sml"] > val it = () : unit > val it = () : unit > val it = () : unit > val it = () : unit list=1,2,3,4 sumpairs=3,7
We add compile
to the above command in order to generate and execute the SML image target\sumpairs-image.x86-win32
:
> build -verbose clean compile run Compile SML source file "sumpairs" Standard ML of New Jersey [Version 110.99.6.1; 32-bit; October 25, 2024] [scanning ../build.cm] [parsing ../(build.cm):src/sumpairs.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/sumpairs.sml] [...] [code: 338, data: 78, env: 39 bytes] Execute SML image "target\sumpairs-image.x86-win32" list=1,2,3,4 sumpairs=3,7