Skip to main content

SML Basics Examples

By Eunice Chen, May 2020

Types

For each of the following declarations, state the type and value of x.

val x = 1 > 5
val x = 15 div 0
val x = 15.0 div 0.0
val x = fn (n : int) => Int.toString n
val x = fn n => ("1" ^ "5") ^ (Int.toString n)

Scope

Example 0

let
val y : int = 2
in
fn (x : int) => z*z
end

What is the value of the let-in-end expression?

Example 1

val y = 0
val z =
(let
val x : int = 1
fun f (x : int) = x
val y : int = x + 1
in
fn (a : string) => x*2
end) "150"
val a = y

What is the value of y before the let-in-end expression?

What is the value of y within the let-in-end expression?

What is the value of a?

What is the value of z?

Example 2

val x : int = 1
fun f (x : int) = x + 1
val y : int = 2
val z : int = f y
val a : int = x

What are the values of x, y, z, and a?

Example 3

val x = 1
val y = 2
fun f (x, y) =
case x of
0 => 0
| y => y
val a = f (y, x)
val b = f (x, y)

What are the values of a and b?