Prolog factorial recursion

后端 未结 8 1916
無奈伤痛
無奈伤痛 2021-01-11 12:16

I\'m having trouble understanding the following factorial program

fact1(0,Result) :-
    Result is 1.
fact1(N,Result) :-
    N > 0,
    N1 is N-1,
    fac         


        
8条回答
  •  庸人自扰
    2021-01-11 12:54

    A simple way :

     factorial(N, F):- N<2, F=1.
    
     factorial(N, F) :- 
         M is N-1, 
         factorial(M,T), 
         F is N*T.
    

提交回复
热议问题