Friday, January 16, 2009

Ch1, Ex2

%(a)

declare
fun {Fact N}
if N == 0 then 1 else N*{Fact N-1} end
end

declare
fun {PartialFact N R} % N > R > 0
% local variables to be used must be declared
% in the start
F in
fun {F K}
if K==(N-R+1) then K else K*{F K-1} end
end
{F N}
end

declare
fun {FastComb N R}
if R > 0 then {PartialFact N R} div {Fact R}
else N end
end

%(b)
declare
fun {FasterComb N R}
if R > (N div 2) then {FastComb N (N-R)} else {FastComb N R} end
end

{Browse {FasterComb 5 3}} %10
{Browse {FasterComb 5 0}} %5

No comments:

Post a Comment