Prolog factorial recursion

后端 未结 8 1832
無奈伤痛
無奈伤痛 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
    factorial(1, 1).
    factorial(N, Result) :- M is N - 1,
    factorial(M, NextResult), Result is NextResult * N.
    
    0 讨论(0)
  • 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.
    
    0 讨论(0)
提交回复
热议问题