Prolog Tutorial

 
 
 

Prolog Basics


File and Database Programs

Spiral game Relation max(X,Y,Max) Display each digit with respective frequencie Family hierarchy Person hobbies Predicate order (List) is true if List is ordered String in increasing order of their length in db Rel last(item,list) so item is last el of list Convert decimal number to roman number Find animals leaving in water and land Defines predicate maxlist (L,Max) Add element in last position in given list Predicate logic representation and prove proof Search student data - Name or phone no is i/p

Game

Game Predicates evenlength(List) and oddlength(List) Multi-List Program Run marriage burro Find names of student obtained marks greater Find length of given list DB of teacher - show no of lectures require Relation translate L1, L2 to num b/w 0 and 9 Read file and create list of all the lines of it Add and multiply numbers Find grade Concate , member in list, reverse, delete list Read number Parent Tree Search company, car name, price, class, color Relation dividelist (L,L1,L2) Find factorial of given number Create window i/p string, remove the window & text Reverse list using concatenate Check number is member of given list or not 10 int in list,10 from database - find 2nd max

Search Programs

Game LT Find grade Find relations Insert an element anywhere in the list Display item name, code, quantity, price,grade Find easy courses person likes Logic repres - converting to prove the proof Define relation shift L1,L2- L1 shift by one left Read file display line by line - go to output file Delete an element from list using concatenate Read 10 strings into a list & display similar char Parent and predecessor Check given word is a palindrome or not Delete an element from given list Copy String length equal to number using file Sort list of integers using Insertion Sort Find maximum number from inputted number Monkey and banana Find nth element of a list Generate first 10 prime num store them in list Blocks problem using hill climbing Find last item of the list Check whether given list is palindrome or not Store cricket players details in database Relation last(item,list) so last el w/o concate Ask question Buy and sale cars Merge two ordered list Print number from 1 to 25 Murder mystery 8puzzle - heuristic function % - best first search

Problem Solving

Game 5*5 matrix -moving in spiral way- dice value Fibonacci Series Read two strings concatenation of the both Production system - Missionary cannibal problem Print zodiac sign of a given birth date Use of list DB Employees - Print emp children of given age Add an element to the begining of list Print message Find relations in family Login page Add element in head position in given list Water Jug Problem start with state(0,0) end (2,0) Search record by name and phone number

Sort Programs

Game EX Find length of a list is even or odd String token Generate facts of family - take different relation Relation split(L,P,N) Find sum of all numbers in given list Show no of user entered book - 2 diff database Program to print user entered number Marcus Check year is a leap year or not Compare character and string Medical Diagnostic system Rotate list n places to the left Define relation shift L1,L2- L1 shift by one right Read sentence from file copy each of token in list Delete el from given list w/o using concatenate Display strings with closest length of int entered Find murderer Find permutation of a given list Find last element in a given list Read file and create a list of all words of it

Basic Programs

Sort list using Bubble Sort Find length of names Teacher and lecture Generate sublist of a given list Compare characters, strings, reverse string 4-3 Gallon Water Jug Problem Split list in negative & positive list Reverse given list Read and write file Relation count(A,L,N) num of occurrences Find summation of list of numbers Buy car and truck Write elements of the list line by line Concatenate two lists in giving third list Copy in database - string length equal to number Sort list using Quick Sort
 
 
domains
    leftX,leftY,rightX,rightY = integer
database
    go(integer,integer,integer,integer)
predicates
    travel(integer,integer,integer,integer)
clauses
    travel(LeftX,LeftY,RightX,RightY) :- 
                        LeftX = 0,
                        LeftY = 0,
                        RightX = 3,
                        RightY = 3,
                        write(\"\\nProblem is solved.\").
    travel(LeftX,LeftY,RightX,RightY) :- 
                        LeftX = 3,
                        LeftY = 3,
                        NewLeftX = 2,
                        NewLeftY = 2,
                        NewRightX = RightX,
                        NewRightY = RightY,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write(\"\\nLeft : cannibals : \" , NewLeftX, \" Missionaries : \", NewLeftY),
                        Write(\" Right : cannibals : \" , RightX, \" Missionaries : \", RightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).

    travel(LeftX,LeftY,RightX,RightY) :- 
                        RightX = 0,
                        RightY = 0,
                        NewLeftX = LeftX,
                        NewLeftY = LeftY,
                        NewRightX = 0,
                        NewRightY = 1,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write(\"\\nLeft : cannibals : \" , LeftX, \" Missionaries : \", LeftY),
                        Write(\" Right : cannibals : \" , NewRightX, \" Missionaries : \", NewRightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).

    travel(LeftX,LeftY,RightX,RightY) :- 
                        LeftX = 2,
                        LeftY = 2,
                        NewLeftX = 3,
                        NewLeftY = 0,
                        NewRightX = RightX,
                        NewRightY = RightY,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write(\"\\nLeft : cannibals : \" , NewLeftX, \" Missionaries : \", NewLeftY),
                        Write(\" Right : cannibals : \" , RightX, \" Missionaries : \", RightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).

    travel(LeftX,LeftY,RightX,RightY) :- 
                        RightX = 0,
                        RightY = 1,
                        NewLeftX = LeftX,
                        NewLeftY = LeftY,
                        NewRightX = 0,
                        NewRightY = 2,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write(\"\\nLeft : cannibals : \" , LeftX, \" Missionaries : \", LeftY),
                        Write(\" Right : cannibals : \" , NewRightX, \" Missionaries : \", NewRightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).

    travel(LeftX,LeftY,RightX,RightY) :- 
                        LeftX = 3,
                        LeftY = 0,
                        NewLeftX = 1,
                        NewLeftY = 1,
                        NewRightX = RightX,
                        NewRightY = RightY,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write(\"\\nLeft : cannibals : \" , NewLeftX, \" Missionaries : \", NewLeftY),
                        Write(\" Right : cannibals : \" , RightX, \" Missionaries : \", RightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).

    travel(LeftX,LeftY,RightX,RightY) :- 
                        RightX = 0,
                        RightY = 2,
                        NewLeftX = LeftX,
                        NewLeftY = LeftY,
                        NewRightX = 1,
                        NewRightY = 1,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write(\"\\nLeft : cannibals : \" , LeftX, \" Missionaries : \", LeftY),
                        Write(\" Right : cannibals : \" , NewRightX, \" Missionaries : \", NewRightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).


    travel(LeftX,LeftY,RightX,RightY) :- 
                        LeftX = 1,
                        LeftY = 1,
                        NewLeftX = 0,
                        NewLeftY = 2,
                        NewRightX = RightX,
                        NewRightY = RightY,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write(\"\\nLeft : cannibals : \" , NewLeftX, \" Missionaries : \", NewLeftY),
                        Write(\" Right : cannibals : \" , RightX, \" Missionaries : \", RightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).

    travel(LeftX,LeftY,RightX,RightY) :- 
                        RightX = 1,
                        RightY = 1,
                        NewLeftX = LeftX,
                        NewLeftY = LeftY,
                        NewRightX = 3,
                        NewRightY = 0,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write(\"\\nLeft : cannibals : \" , LeftX, \" Missionaries : \", LeftY),
                        Write(\" Right : cannibals : \" , NewRightX, \" Missionaries : \", NewRightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).


    travel(LeftX,LeftY,RightX,RightY) :- 
                        LeftX = 0,
                        LeftY = 2,
                        NewLeftX = 0,
                        NewLeftY = 1,
                        NewRightX = RightX,
                        NewRightY = RightY,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write(\"\\nLeft : cannibals : \" , NewLeftX, \" Missionaries : \", NewLeftY),
                        Write(\" Right : cannibals : \" , RightX, \" Missionaries : \", RightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).
    travel(LeftX,LeftY,RightX,RightY) :- 
                        RightX = 3,
                        RightY = 0,
                        NewLeftX = LeftX,
                        NewLeftY = LeftY,
                        NewRightX = 3,
                        NewRightY = 1,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write(\"\\nLeft : cannibals : \" , LeftX, \" Missionaries : \", LeftY),
                        Write(\" Right : cannibals : \" , NewRightX, \" Missionaries : \", NewRightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).

    travel(LeftX,LeftY,RightX,RightY) :- 
                        LeftX = 0,
                        LeftY = 1,
                        NewLeftX = 0,
                        NewLeftY = 0,
                        NewRightX = RightX,
                        NewRightY = RightY,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write(\"\\nLeft : cannibals : \" , NewLeftX, \" Missionaries : \", NewLeftY),
                        Write(\" Right : cannibals : \" , RightX, \" Missionaries : \", RightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).
    travel(LeftX,LeftY,RightX,RightY) :- 
                        RightX = 3,
                        RightY = 1,
                        NewLeftX = LeftX,
                        NewLeftY = LeftY,
                        NewRightX = 3,
                        NewRightY = 3,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write(\"\\nLeft : cannibals : \" , LeftX, \" Missionaries : \", LeftY),
                        Write(\" Right : cannibals : \" , NewRightX, \" Missionaries : \", NewRightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).

goal
    makewindow(1,2,3,\"Cannibals - missionaries problem\",0,0,25,80),
    Write(\"\\nLeft : cannibals : 3\" ,  \" Missionaries : 3\" ),    
    Write(\" Right : cannibals : 0\" ,  \" Missionaries : 0\" ),
    travel(3,3,0,0).

    Related Post:
  1. ProLog Program to search car by company name, car name, price, class, color...

  2. Prolog program of Medical Diagnostic system

  3. Prolog program Define the relation last(item,list) so that item is the last element of the list using concatenate

  4. ProLog Program to find animals leaving in water and land

  5. Prolog program to find the length of a given list

  6. ProLog Program to read list of strings and numbers and copy in database having having string length equal to number using files

  7. Prolog program of Water Jug Problem start with state(0,0) and end with(2,0)

  8. Prolog program that defines a relation max(X,Y,Max) where Max is the greater of X and Y

  9. Prolog program to define the relation translate(L1,L2) to translate a list of numbers 0-9 to a list of corresponding words i.e. 0 - zero 1 - one

  10. Prolog program of spiral game

  11. ProLog Program to find length of names

  12. Prolog program to find factorial of given number

  13. Prolog program to define two predicates evenlength(List) and oddlength(List) so that they are true if their argument is a list of even or odd length

  14. ProLog Program to print message

  15. Prolog program to generate first 10 prime numbers and store them in a list

  16. ProLog Program to generate facts of the family and take different relations

  17. Prolog program to find the nth element of a list

  18. Prolog program to check whether a number is a member of given list or not

  19. Prolog program to define the relation shift(L1,L2) so that L2 is L1 shifted rotationally by one element to the left

  20. Prolog program to display item name, code, quantity, price and grade

 
 
Didn't find what you were looking for? Find more on Production system for the Missionary cannibal problem