Skip to content

Instantly share code, notes, and snippets.

View kunishi's full-sized avatar

Takeo Kunishima kunishi

View GitHub Profile
fun interleave n nil = [[n]]
| interleave n (L as x::xs) = (n::L) :: map (fn L => x::L) (interleave n xs);
fun intersect(nil, _) = nil
| intersect(_, nil) = nil
| intersect(xa as x::xs, ya as y::ys) =
if x = y then x::intersect(xs, ys)
else if x < y then intersect(xs, ya)
else intersect(xa, ys) (* x > y *);
fun length(nil) = 0
| length(x::xs) = 1 + length(xs);
fun map(F, nil) = nil
| map(F, x::xs) = F(x)::map(F,xs);
fun maxList(L) =
if tl(L) = nil
then hd(L)
else
let
val x = hd(L);
val y = maxList(tl(L))
in
if x<y then y else x
end;
fun merge(nil, M) = M
| merge(L, nil) = L
| merge(x::xs, y::ys) =
if (x:int) < y then x::merge(xs, y::ys)
else y::merge(x::xs, ys);
fun merge(nil, M) = M
| merge(L, nil) = L
| merge(L as x::xs, M as y::ys) =
if (x:int) < y then x::merge(xs, M)
else y::merge(L, ys);
fun mergeSort(nil) = nil
| mergeSort([a]) = [a]
| mergeSort(L) =
let
val (M, N) = split(L);
val M = mergeSort(M);
val N = mergeSort(N);
in
merge(M, N)
end;
fun millionPower(x:real) =
let
val four = x*x*x*x;
val twenty = four*four*four*four*four;
val hundred = twenty*twenty*twenty*twenty*twenty;
val fivehundred = hundred*hundred*hundred*hundred*hundred
in
fivehundred*fivehundred
end;
fun min3(a:int, b, c) =
if a<b then
if a<c then a
else c
else
if b<c then b
else c;