Logo 
Search:

Artificial Intelligence Articles

Submit Article
Home » Articles » Artificial Intelligence » ProLogRSS Feeds

Prolog program to define the relation shift(List1, List2) so that List1 shifts rotationally by one element to the right

Posted By: Milind Mishra     Category: Artificial Intelligence     Views: 2888

Define the relation shift(List1, List2) so that List1 shifts
rotationally by one element to the right.
E.g. if the input is shift([1,2,3,4,5], L) then
the output should be [5,1,2,3,4].

Code for Prolog program to define the relation shift(List1, List2) so that List1 shifts rotationally by one element to the right in Artificial Intelligence

domains

    list=integer*

    
predicates

    shift(list,list)
    del(list,list,integer)
    add(list,integer,list)

    
clauses

    shift(L1,L2):-
        del(L1,L3,X),
        add(L3,X,L2).
        
    del([X],[],X).
    
    del([Head|Tail],[Head|L],X):-
        del(Tail,L,X).
        
    add(L3,X,[X|L3]).

        
OUT PUT
=======

Goal: shift([1,2,3,4,5],List)

List=[5,1,2,3,4]

1 Solution

-------------------------------------

Goal: shift([1,2,3,4,5,6,7,8,9],List)

List=[9,1,2,3,4,5,6,7,8]

1 Solution
  
Share: 



Milind Mishra
Milind Mishra author of Prolog program to define the relation shift(List1, List2) so that List1 shifts rotationally by one element to the right is from India.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!