Factorial in Prolog and C++
- by Joshua Green
I would like to work out a number's factorial. My factorial rule is in a Prolog file and I am connecting it to a C++ code. Can someone tell me what is wrong with my C++ interface please?
% factorial.pl
factorial( 1, 1 ):-
!.
factorial( X, Fac ):-
X > 1,
Y is X - 1,
factorial( Y, New_Fac ),
Fac is X * New_Fac.
// factorial.cpp
# headerfiles
term_t t1;
term_t t2;
term_t goal_term;
functor_t goal_functor;
int main( int argc, char** argv )
{
argc = 4;
argv[0] = "libpl.dll";
argv[1] = "-G32m";
argv[2] = "-L32m";
argv[3] = "-T32m";
PL_initialise(argc, argv);
if ( !PL_initialise(argc, argv) )
PL_halt(1);
PlCall( "consult(swi('plwin.rc'))" );
PlCall( "consult('factorial.pl')" );
cout << "Enter your factorial number: ";
long n;
cin >> n;
PL_put_integer( t1, n );
t1 = PL_new_term_ref();
t2 = PL_new_term_ref();
goal_term = PL_new_term_ref();
goal_functor = PL_new_functor( PL_new_atom("factorial"), 2 );
PL_put_atom( t1, t2 );
PL_cons_functor( goal_term, goal_functor, t1, t2 );
PL_halt( PL_toplevel() ? 0 : 1 );
}