Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prolog..interchanging pairs in the list!

Status
Not open for further replies.

Yupar

Programmer
Mar 11, 2004
4
AU
How can I write a predicate that takes a List like [1,2,3,4,5,6] and binds NewList to a list formed by interchanging pairs of members of the list, so in the example, NewList would be [2,1,4,3,6,5]. If the list has an odd number, then the final one is left unchanged at the end of the NewList.

Eg:
:spin([cat,dog,fish],X)?
X=[dog,cat,fish]


the english version:

If List is empty, SpunList is also empty.
If List is one element, SpunList is a list of that same element.
If List has two or more elements (in prolog, List = [A,B|Rest]),
SpunList is constructed by swapping A and B, then spinning the Rest.

Thanks.

Regards,
Yupar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top