% From the book % PROLOG PROGRAMMING IN DEPTH % by Michael A. Covington, Donald Nute, and Andre Vellino % (Prentice Hall, 1997). % Copyright 1997 Prentice-Hall, Inc. % For educational use only % File PRINTABL.PL % Prints a table of a mathematical function % and demonstrates passing procedures as arguments % print_table(+LambdaExpression,+Start,+Finish,+Step) % Prints a table of the mathematical function computed by % LambdaExpression, with the input argument ranging % from Start to Finish and incrementing by Step. print_table(_,Start,Finish,_) :- Start > Finish. % Stop when Start exceeds Finish print_table(lambda(Input,Result,Goal),Start,Finish,Step) :- write(Start), write(' '), prove_and_discard_instantiations( ( Input = Start, call(Goal), write(Result), nl ) ), NewStart is Start + Step, print_table(lambda(Input,Result,Goal),NewStart,Finish,Step). % prove_and_discard_instantiations(+Goal) % Executes Goal, but discards any instantiations performed. prove_and_discard_instantiations(Goal) :- \+ \+ call(Goal). % Demonstration predicates test1 :- print_table(lambda(X,Y,(Y is sin(cos(X)))), 0.0, 1.0, 0.1). test2 :- print_table(lambda(X,Y,(Y is 2+X)), 0.0, 1.5, 0.2).