I was having toruble with writing a combinations function in prolog, which follows the following math equation: b! / ((b-c)! * c!) I am new to prolog and not sure what is entirely wrong with it. The error I receive is: ERROR: is/2: Arithmetic: `fact1/2' is not a function
Below is my code:
%fac(0,1).
%fac(N,X) :- N > 0, M is N - 1, fac(M,Y), X is Y * N.
fact1(0,Result) :- Result is 1.
fact1(N,Result) :- N > 0, N1 is N-1,
fact1(N1,Result1), Result is Result1*N.
main :- current_prolog_flag(argv,[BB_S,CC_S]),
atom_number(BB_S,BB),
atom_number(CC_S,CC),
%read_input,
R1 is 1,
R2 is 1,
R3 is 1,
FB is fact1(BB,R1),
ABS is abs(BB - CC),
FE is fact1(ABS,R2),
FC is fact1(CC,R3),
TI is FE * FC,
BF is FB / TI,
%BF is fac(BB) / (fac(abs(BB-CC)) * fac(CC))),
write($BF),
halt.
Thank you for your help.
Never mind. I found a solution. But, for others benefit, I have posted the solution here. Apparently the functions dont have a return and return by placing the value they generate in a given variable.
%fac(0,1).
%fac(N,X) :- N > 0, M is N - 1, fac(M,Y), X is Y * N.
fact1(0,Result) :- Result is 1.
fact1(N,Result) :- N > 0, N1 is N-1, fact1(N1,Result1), Result is Result1*N.
main :- current_prolog_flag(argv,[BB_S,CC_S]),
atom_number(BB_S,BB),
atom_number(CC_S,CC),
%read_input,
fact1(BB,FB), %notice change
ABS is abs(BB - CC),
fact1(ABS,FE), %notice change
fact1(CC,FC), %notice change
TI is FE * FC,
BF is FB / TI,
%BF is fac(BB) / (fac(abs(BB-CC)) * fac(CC))),
print('Number of bracelets: '),
write(BF),
halt.