简体   繁体   中英

Defining predicates in prolog

Using prolog,

  1. Define the predicate, conc(L1,L2,L3), where L3 is a concatenation of the two lists L1 and L2
  2. Using above predicate, define the predicate One_after_first(X,L) is true iff item X occurs immediately after the first element in list L.
conc(L1, L2, L3) :- append(L1, L2, L3).

one_after_first(L, Y) :- conc([_ | [Y | []]], _, L).

This solution uses conc , but there is an easier solution:

one_after_first([_ | [Y | _]], Y).

You can call it with:

one_after_first([1,2,3,4], 2).

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