51 lines
1.0 KiB
Plaintext
51 lines
1.0 KiB
Plaintext
fn identity<T>(x: T): T = x
|
|
|
|
type Pair<A, B> =
|
|
| MkPair(A, B)
|
|
|
|
fn first<A, B>(p: Pair<A, B>): A =
|
|
match p {
|
|
MkPair(a, _) => a,
|
|
}
|
|
|
|
fn second<A, B>(p: Pair<A, B>): B =
|
|
match p {
|
|
MkPair(_, b) => b,
|
|
}
|
|
|
|
fn mapOption<T, U>(opt: Option<T>, f: fn(T): U): Option<U> =
|
|
match opt {
|
|
None => None,
|
|
Some(x) => Some(f(x)),
|
|
}
|
|
|
|
fn double(x: Int): Int = x * 2
|
|
|
|
let id_int = identity(42)
|
|
|
|
let id_str = identity("hello")
|
|
|
|
let pair = MkPair(1, "one")
|
|
|
|
let fst = first(pair)
|
|
|
|
let snd = second(pair)
|
|
|
|
let doubled = mapOption(Some(21), double)
|
|
|
|
fn showOption(opt: Option<Int>): String =
|
|
match opt {
|
|
None => "None",
|
|
Some(x) => "Some(" + toString(x) + ")",
|
|
}
|
|
|
|
fn printResults(): Unit with {Console} = {
|
|
Console.print("identity(42) = " + toString(id_int))
|
|
Console.print("identity(\"hello\") = " + id_str)
|
|
Console.print("first(MkPair(1, \"one\")) = " + toString(fst))
|
|
Console.print("second(MkPair(1, \"one\")) = " + snd)
|
|
Console.print("map(Some(21), double) = " + showOption(doubled))
|
|
}
|
|
|
|
let output = run printResults() with {}
|