Skip to content

Instantly share code, notes, and snippets.

View kunishi's full-sized avatar

Takeo Kunishima kunishi

View GitHub Profile
fun duplicate(L) =
if L=nil then nil
else hd(L)::hd(L)::duplicate(tl(L));
fun duplicate(nil) = nil
| duplicate(x::xs) = x::x::duplicate(xs);
fun reverse(nil) = nil
| reverse(x::xs) = reverse(xs)@[x];
fun ends(L) =
[hd(L), hd(reverse(L))];
fun eval(nil, a) = 0.0
| eval(p::ps, a) = p + eval(ps, a)*a;
fun evenel(nil) = nil
| evenel(nil::zzs) = evenel(zzs)
| evenel([x]::zzs) = evenel(zzs)
| evenel((x::y::zs)::zzs) = y::evenel(zs::zzs);
fun exists(p, nil) = false
| exists(p, x::xs) = p(x) orelse exists(p, xs);
open TextIO;
exception Syntax;
fun digit(c) = (#"0" <= c andalso c <= #"9");
fun integer(IN, i) =
case lookahead(IN) of
SOME c =>
if digit(c) then
(input1(IN);
fun factorial(n) =
if n = 1 then 1
else n * factorial(n-1);
fun factorial n =
let
fun fact1 1 result = result
| fact1 n result = fact1 (n-1) (n * result)
in
fact1 n 1
end;
fun factorial_pat(1) = 1
| factorial_pat(n) = n * factorial_pat(n-1);