简体   繁体   English

Prolog三明治谓词

[英]Prolog sandwich predicate

I need to make a predicate sandwich(L) which is true if it has the same elements at the beginning and end of the list, like this: 我需要制作一个谓词sandwich(L) ,如果它在列表的开头和结尾具有相同的元素,则为true,如下所示:

sandwich([a,a,b,c,d,a,a]).

Could you please help me? 请你帮助我好吗?

Many thanks! 非常感谢!

It is not quite clear what a "sandwich" should be, but this is probably the purpose of your assignment. 还不清楚“三明治”应该是什么,但这可能是您分配作业的目的。 After all, most requirements are never that clearcut. 毕竟,大多数需求绝不是那么明确。 Using a DCG seems to be a good idea. 使用DCG似乎是一个好主意。 It might be 有可能

sandwich(X) :-
   phrase(sandwich).

sandwich --> [X], ..., [X].

... --> [] | [_], ... .

or 要么

sandwich --> [X,X], ... , [X,X].

or maybe both. 或两者兼而有之。 And is [X] a sandwich, too? 而且[X]也是三明治吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM