pl_2f_bf_ac_bd_c0_b9_ae_c1_a6
'전자 정보 공학부 200012911 박동희'
fun timeit (f: int->int, n:int) = let open Time open Timer val start = startRealTimer() val result = f(n) val finish = checkRealTimer(totalRealTimer()) in print(toString(finish)); print "\n"; result end; fun fact 0 = 1 | fact(n) = if n=0 then 1 else n*fact(n-1)
fun timeit (f: 'a list->'a list list, n:'a list) = let open Time open Timer val start = startRealTimer() val result = f(n) val finish = checkRealTimer(totalRealTimer()) in print(toString(finish)); print "\n"; result end fun pre(x, nil) = nil | pre( x, L::LS) = (x::L)::pre(x,LS) fun pow([]) = [[]] |pow(X::XS)= let val ps = pow(XS) in ps@pre(X,ps) end
fun rev(x: 'a * 'a * 'a) =
(#3(x), #2(x), #1(x))
fun rev(a, b, c) = (c, b, a);(* *)
fun min3(a,b,c) =
if a<b then
if a<c then a
else c
else
if b<c then c
else c
fun maxtuple(x:int*int*int) =
if #1(x) > #2(x) then
if #1(x) > #3(x) then #1(x)
else #3(x)
else
if #2(x) < #3(x) then #3(x)
else #3(x);
fun sortedList(l:int*int*int) = (* sort the list *)
if #1(l) <= #2(l) then
if #2(l) <= #3(l) then l
else if #1(l) <= #2(l) then (#1(l),#3(l),#2(l))
else (#3(l),#1(l),#2(l))
else
if #2(l) <= #3(l) then
if #1(l) <= #3(l) then (#2(l),#1(l),#3(l))
else (#2(l), #3(l), #1(l))
else (#3(l), #2(l), #1(l))
fun sortedList(a,b,c) = (* sort the list *)
if a <=b then
if b <=c then (a,b,c)
else if a <=c then (a,c,b)
else (c,a,b)
else
if b <=c then
if a <=c then (b,a,c)
else (b,c,a)
else (c, b, a);
fun round(n:real) = (* round real number *)
if n < real(floor(n+0.5)) then real(ceil(n))
else real(floor(n));
(*
fun round(n:string) =
if hd(explode n) = #"." then (floor((real(implode(tl(explode n))))/100 + 0.05))
else round((implode(tl(explode n))))
*)
fun lengthlist(l) =
if l = nil then 0
else 1 + lengthlist(tl(l));
(*
fun lengthiter(list) = (* length- iter version *)
fun iter(l, c) =
if l = nil then c
else iter(tl(l), 1+c)
iter(list, 0); *)
fun removeElement(h::t, 0) = t
| removeElement(h::t, n) =
if lengthlist(h::t) > n then h::removeElement(t,n-1)
else h::t
fun cycleList(h::nil) = h::nil
| cycleList(h::t) = tl(h::t)@[hd(h::t)];
fun cycleListith(h::t, 0) = h::t
| cycleListith(h::t, i) = cycleListith(tl(h::t)@[hd(h::t)], i-1);
fun cycleListith(h::t, 0) = h::t (* high-order function version *)
| cycleListith(h::t, i) = cycleListith(cycleList(h::t), i-1);
fun cycleListith(h::t, 0) = h::t
| cycleListith(h::t, i) =
if lengthlist(h::t) = i then h::t
else cycleListith(tl(h::t)@[hd(h::t)], i-1)
fun cycleList(h::t,i) = (* recursive version *)
if i = 0 then h::t
else cycleList(t@[h], i-1);
pl_2f_bf_ac_bd_c0_b9_ae_c1_a6.txt · Last modified: 2018/07/18 14:10 by 127.0.0.1