I am getting an error in a simple factorial

2014-03-16
2014-03-16
  • factorial01(0,1).
    
    factorial01(N,F) :-
        N>0,
        N1 is N-1,
        factorial01(M1,F1),
        F is N * F1.
    

    This compiles fine and when called like this:

    var W = new Variable();
    foreach (var b in Factorial_01.factorial01(3, W)) Console.WriteLine(W);
    

    it throws an exception: Expected a number but the argument is an unbound variable

    however this factorial works fine, compiles and runs as expected:

    factorial02(0,F,F).
    
    factorial02(N,A,F) :-  
        N > 0, 
        A1 is N*A, 
        N1 is N -1, 
        factorial02(N1,A1,F).
    

    when called with:

    var F = new Variable();
    foreach (var b in Factorial_01.factorial02(5, 1, F)) Console.WriteLine(F);
    

    Produces the answer 120 with no exception. Any idea why this is?