Factorial in C++ and Prolog

This is a discussion on Factorial in C++ and Prolog within the C++ Programming forums, part of the General Programming Boards category; I would like to work out a number's factorial. My factorial rule is in a Prolog file and I am ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    26

    Factorial in C++ and Prolog

    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?

    Code:
        % 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 );
        }
    Edited by moderator: Added code tags
    Last edited by VirtualAce; 04-05-2010 at 04:20 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,166
    You're going to have to give more information. Then someone who is familiar with your setup may be able to advise. The method of interfacing between C (or C++) and prolog is highly dependent on both the compiler and on the prolog interpreter.

    Overwriting argc and argv (as you have done, before the prolog-related stuff) is often an effective way to crash a program. Better to set up your own local storage for argv, and pass that to PL_initialise().

    It would probably be easier to write your own factorial function in C++, but I assume your purpose here is learning to interface with your prolog rather than avoiding coding in C++ (as interfacing to other languages is not trivial).
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21