Skip to content

Instantly share code, notes, and snippets.

View kunishi's full-sized avatar

Takeo Kunishima kunishi

View GitHub Profile
fun fibonacci(n) =
if n = 1 then 1
else if n = 2 then 1
else fibonacci(n-1) + fibonacci(n-2);
fun fib n =
let
fun fib1 1 = (1, 0)
| fib1 n =
let
val (l, m) = fib1 (n-1)
in
(l + m, l)
end
in
fun filter(P, nil) = nil
| filter(P, x::xs) =
if P(x)
then x::filter(P,xs)
else filter(P,xs);
datatype 'a btree = Empty | Node of 'a * 'a btree * 'a btree;
fun find_in_btree(x, Empty) = false
| find_in_btree(x, Node(a, l, r)) =
x = a orelse find_in_btree(x, l) orelse find_in_btree(x, r);
fun flatten(nil) = nil
| flatten(nil::ys) = flatten(ys)
| flatten((xa as x::xs)::ys) = xa @ flatten(ys);
fun flatten nil = nil
| flatten (x::xs) = x @ flatten(xs);
exception NotFound;
fun getID(person, nil) = raise NotFound
| getID(person, (x as {name=p, ...})::xs) =
if p = person then
#ID(x:{name:string, ID:int, courses:string list})
else getID(person, xs);
datatype 'label btree =
Empty |
Node of 'label * 'label btree * 'label btree;
fun height(Empty) = 0
| height(Node(x, l, r)) =
let
val hl = height(l);
val hr = height(r)
in
fun hundredthPower(x:real) =
let
val four = x*x*x*x;
val twenty = four*four*four*four*four
in
twenty*twenty*twenty*twenty*twenty
end;
fun identity(x) = x;