Factorial

Description

Factorial

Source: The Art of Prolog

Download

Program source code: factorial.pl

Listing

/*
 
	factorial(N,F) :- F equals N factorial.
 
*/
 
 
 
	factorial(0,s(0)).
 
	factorial(s(N),F) :- factorial(N,F1), times(s(N),F1,F).
 
 
 
	times(0,X,0).
 
	times(s(X),Y,Z) :- times(X,Y,XY), plus(XY,Y,Z).
 
 
 
	plus(0,X,X) :- natural_number(X).
 
	plus(s(X),Y,s(Z)):- plus(X,Y,Z).
 
 
 
	natural_number(0).
 
	natural_number(s(X)) :- natural_number(X).
 
 
 
%	Program 3.6: Computing fa
ctorials

Comments

RTERR: No permission to redefine built-in predicate 'plus/3'

pl/prolog/pllib/factorial.txt · ostatnio zmienione: 2017/07/17 08:08 (edycja zewnętrzna)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0