Prolog 中的 IF 语句 - 条件 [英] IF Statement in Prolog - Conditional
问题描述
我刚刚完成将一些变量传递给另一个方法并将它们与事实相匹配.我现在想以 if 语句的形式添加一些安全性.我希望检查一下用户输入的变量是否有效(这是数字形式)
I have just completed passing some variables down to another method and matching them to facts. I now want to add some security in the form of an if statement. I am hoping to check to see the variable inputted by the user is valid (this is in the form of a number)
下面的代码显示了一个例子,基本上用户选择了 1 到 10 之间的一个选项,如果用户输入了一个无效的选项(即 11),我希望该方法打印出无效的选择"并重复方法.
An example of this is shown in the code below, basically a user chooses an option between 1 and 10, if the user inputs an invalid option (ie 11) I want the method to print out "invalid choice" and repeat the method.
我的问题:如何在Prolog代码中实现if语句,下面显示的代码之前的方法会传入一个数字
My question: How do I implement an if statement into Prolog code, the method before the code shown below would pass in a number
ifStatment(X) :-
write("Here is your list"),
nl,nl,
forall(listOfStuff(X,Text), writeln(Text)),
read(Y),
推荐答案
问题的标题 IF Statement in Prolog
使用了大多数程序员都会提到的词 if
if 语句的概念.在逻辑编程方面,if
这个词提出了条件 ->/2 或带有保护语句的谓词.这个答案演示了在同一问题上的两种方法,结果相同.
The title of the question IF Statement in Prolog
uses the word if
which to most programmers brings up the concept of an if statement. In terms of logic programming the word if
brings up the concept of the conditional ->/2 or predicates with guard statements. This answer demonstrates both ways on the same problem with the same result.
Extending from your previous question and the accepted answer.
第一种方法使用谓词 valid_1(N)
来检查输入是否有效.这不使用 ->/2
而是使用两个子句谓词和相互独立的保护语句.
The first way uses the predicate valid_1(N)
to check if the input is valid. This does not use ->/2
but uses a two clause predicate with mutually independent guard statements.
第一个子句保护语句是:
The first clause guard statements are:
(0 < N, N < 11)
注意,
在Prolog中的意思是和
.这个读
Note the use of ,
which means and
in Prolog. This reads
N is greater than 0 AND N is less than 11.
第二个子句保护语句是:
The second clause guard statements are:
(N < 1; N > 10)
注意 ;
在 Prolog 中表示 或
的使用.这个读
Note the use of ;
which means or
in Prolog. This reads
N is less than 1 OR N is greater than 10.
第二种方式使用谓词 valid_2(N)
来检查输入是否有效.这使用 ->/2.
The second way uses the predicate valid_2(N)
to check if the input is valid. This uses ->/2.
使用条件的格式是
(
% condition
->
% true
;
% false
)
这些可以嵌套,这在本例中完成.
these can be nested and that is done in this example.
注意:注释% condition
、% true
和% false
的使用不是必需的.为了清楚起见,我添加了它们.
Note: The use of the comments % condition
, % true
and % false
are not required. I added them for clarity.
valid_2(N) :-
(
% condition
0 < N
->
% true
(
% condition
N < 11
->
% true
writeln("Valid entry.")
;
% false
writeln("Invalid entry. Please try again.")
)
;
% false
writeln("Invalid entry. Please try again.")
).
这是完整的代码片段.要使用 valid_1
或 valid_2
进行更改,只需使用 %
对其中一个进行注释.
Here is the complete code snippet.
To change using either valid_1
or valid_2
just make one or the other a comment using %
.
tell_me_your_problem:-
output_problems,
read_input.
output_problems :-
forall(problem(P),
writeln(P)).
read_input :-
repeat,
read_string(user_input, "\n", "\r\t ", _, Line),
process_input(Line).
process_input(Line) :-
string(Line),
atom_number(Line, N),
integer(N),
%valid_1(N),
valid_2(N),
do_something_with(Line),
fail.
process_input("quit") :-
write('Finished'), nl,
!, true.
valid_1(N) :-
(0 < N, N < 11),
writeln("Valid entry.").
valid_1(N) :-
(N < 1; N > 10),
writeln("Invalid entry. Please try again.").
valid_2(N) :-
(
% condition
0 < N
->
% true
(
% condition
N < 11
->
% true
writeln("Valid entry.")
;
% false
writeln("Invalid entry. Please try again.")
)
;
% false
writeln("Invalid entry. Please try again.")
).
do_something_with(X) :-
writeln(X).
problem('1').
problem('2').
problem('3').
problem('4').
problem('5').
problem('6').
problem('7').
problem('8').
problem('9').
problem('10').
这是一个示例查询.
?- tell_me_your_problem.
1
2
3
4
5
6
7
8
9
10
|: 11
Invalid entry. Please try again.
11
|: 0
Invalid entry. Please try again.
0
|: 5
Valid entry.
5
|: quit
Finished
true .
这篇关于Prolog 中的 IF 语句 - 条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!