如何比较prolog中的字符串

时间:2012-10-30 20:29:31

标签: string prolog swi-prolog

我想在prolog中编写一个比较两个字符串或字符串列表的程序。我想实现以下目标:

if StringList A == StringList B
   {
     do this
   }
else
   do something else

我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:3)

do this你是什么意思?在Prolog中很难实现doing somewhat,因为你所拥有的只是事实和谓词。

?- (string1 = string2, X=1); (string1 \= string2, X=2).
X = 2.

答案 1 :(得分:2)

以下是您如何在一行中完成的工作:

...
(A = B -> do this ; do something else)
...

答案 2 :(得分:0)

 /*SWI prolog code*/
string1(progga).
string2(ikra).

go:-
write("Enter your name"),
nl,
read(X),nl,
string1(Y),
X=@=Y,nl, write("Matched");
write("not Matched"),go2.

/*Another way to*/
go2:-                
string1(A),
string2(B),
A=@=B,nl, write("Matched");
write("not Matched").
相关问题