简体   繁体   中英

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([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. 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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