Prolog Tutorial

 
 
 

Prolog Basics

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 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 Split list in negative & positive list Reverse given list Read and write file Find length of names Teacher and lecture Generate sublist of a given list Compare characters, strings, reverse string 4-3 Gallon Water Jug Problem Concatenate two lists in giving third list Copy in database - string length equal to number Sort list using Quick Sort 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 Person hobbies Predicate order (List) is true if List is ordered String in increasing order of their length in db

File and Database Programs

Spiral game Relation max(X,Y,Max) Display each digit with respective frequencie Family hierarchy Add element in last position in given list Predicate logic representation and prove proof Search student data - Name or phone no is i/p 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) Find length of given list DB of teacher - show no of lectures require

Game

Game Predicates evenlength(List) and oddlength(List) Multi-List Program Run marriage burro Find names of student obtained marks greater Search company, car name, price, class, color 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 Check number is member of given list or not 10 int in list,10 from database - find 2nd max

Search Programs

Game LT Relation dividelist (L,L1,L2) Find factorial of given number Create window i/p string, remove the window & text Reverse list using concatenate 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 Find grade Find relations Insert an element anywhere in the list Display item name, code, quantity, price,grade Find easy courses person likes Delete an element from given list Copy String length equal to number using file Sort list of integers using Insertion Sort 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 Find last item of the list Check whether given list is palindrome or not Store cricket players details in database 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 Print number from 1 to 25 Murder mystery 8puzzle - heuristic function % - best first search Relation last(item,list) so last el w/o concate Ask question Buy and sale cars Merge two ordered list Print zodiac sign of a given birth date Use of list DB Employees - Print emp children of given age

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 Search record by name and phone number 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) Find sum of all numbers in given list Show no of user entered book - 2 diff database

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) 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 Program to print user entered number Marcus Check year is a leap year or not Compare character and string Medical Diagnostic system
 
 
predicates

    male(symbol).
    female(symbol).
    father(symbol,symbol).
    husband(symbol,symbol).
    brother(symbol,symbol).
    sister(symbol,symbol).
    listbrothers(symbol).
    listsisters(symbol).
    mother(symbol,symbol).
    grandfather(symbol).
    grandmother(symbol).
    uncle(symbol).
    aunt(symbol).
    cousin(symbol).
    listgrandsons(symbol).
    listgranddaughters(symbol).
    printmenu.
    action(integer).
    repeat.


clauses

    male(dashrath).
    male(ram).
    male(laxman).
    male(bharat).
    male(luv).
    male(kush).
    male(son_of_laxman).

    female(kaushalya).
    female(sita).
    female(urmila).    
    female(daughter_of_dashrath).

    father(dashrath,ram).
    father(dashrath,laxman).
    father(dashrath,bharat).
    father(ram,luv).
    father(ram,kush).
    father(laxman,son_of_laxman).
    father(dashrath,daughter_of_dashrath).

    husband(dashrath,kaushalya).
    husband(ram,sita).
    husband(laxman,urmila).

    mother(X,Y):- husband(Z,X),
            father(Z,Y).

    brother(X,Y):- father(Z,X),
            father(Z,Y),
            X<>Y,
            male(X).

    sister(X,Y):- father(Z,X),
            father(Z,Y),
            X<>Y,
            female(X).

    listbrothers(X) :- brother(Z,X),
                write(Z).

    listsisters(X):- sister(Z,X),
                write(Z).

    grandfather(X):- father(Y, Z), 
                father(Z,X),
                write(Y, \" is the grandfather of \",X,\"\\n\").

    grandmother(X):- husband(Z,X),
                father(Z,V),
                father(V,Y),
                write(Y, \" is the grandmother of \",X,\"\\n\").


    listgrandsons(X):- father(X,Z),
                father(Z,Y),
                male(Y),
                write(Y,\"\\n\"),
                fail.

    listgrandsons(X):- husband(Y,X),
                father(Y,V),
                father(V,Z),
                male(Z),
                write(Z,\"\\n\"),
                fail.

    listgranddaughters(X):- father(X,Z),
                father(Z,Y),
                female(Y),
                write(Y,\"\\n\"),
                fail.

    listgranddaughters(X):- husband(Y,X),
                father(Y,V),
                father(V,Z),
                female(Z),
                write(Z,\"\\n\"),
                fail.

    uncle(X):- brother(Z,Y),
            father(Z,X),
            male(Y),
            write(Y,\"\\n\"),
            fail.

    aunt(X):- husband(Z,Y),
            brother(Z,V),
            father(V,X),
            write(Y,\"\\n\"),
            fail.

    cousin(X):- father(Z,X),
            father(V,Y),
            Z<>V,
            brother(V,Z),
            write(Y,\"\\n\").


    repeat.
    repeat:- repeat.

    action(1):-        write(\"\\nEnter name of person whose father is to be found : \"),
            readln(X),
            write(\"\\n\"),
            write(\"Father of \",X,\" is:\"),
            father(Z,X),
            write(Z,\"\\n\"),
            fail.

    action(2):-        write(\"\\nEnter name of person whose mother is to be found : \"),
            readln(X),
            write(\"\\n\"),
            write(\"Mother of \",X,\" is:\"),
            mother(Z,X),
            write(Z,\"\\n\"),
            fail.

    action(3):-        write(\"\\nEnter name of person whose brothers are to be found : \"),
            readln(X),
            write(\"\\n\"),
            write(\"Brothers of \",X,\" are:\\n\"),
            listbrothers(X),
            write(\"\\n\"),
            fail.

    action(4):-        write(\"\\nEnter name of person whose sisters are to be found : \"),
            readln(X),
            write(\"\\n\"),
            write(\"Sisters of \",X,\" are:\\n\"),
            listsisters(X),
            write(\"\\n\"),
            fail.

    action(5):-        write(\"\\nEnter name of person whose grandsons are to be found : \"),
            readln(X),
            write(\"\\n\"),
            write(\"Grandsons of \",X,\" are:\\n\"),
            listgrandsons(X),
            write(\"\\n\"),
            fail.


    action(6):-        write(\"\\nEnter name of person whose granddaughters are to be found : \"),
            readln(X),
            write(\"\\n\"),
            write(\"Granddaughters of \",X,\" are:\\n\"),
            listgranddaughters(X),
            write(\"\\n\"),
            fail.

    action(7):-        write(\"\\nEnter name of person whose uncles are to be found : \"),
            readln(X),
            write(\"\\n\"),
            write(\"Uncles of \",X,\" are:\\n\"),
            uncle(X),
            write(\"\\n\"),
            fail.
    action(8):-        write(\"\\nEnter name of person whose aunties are to be found : \"),
            readln(X),
            write(\"\\n\"),
            write(\"Aunties of \",X,\" are:\\n\"),
            aunt(X),
            write(\"\\n\"),
            fail.


    action(9):-        write(\"\\nEnter name of person whose cousins are to be found : \"),
            readln(X),
            write(\"\\n\"),
            write(\"Cousins of \",X,\" are:\\n\"),
            cousin(X),
            write(\"\\n\"),
            fail.

    action(0).

    printmenu :- 
        repeat,
        write(\"\\n1. Display Father of?\\n\"),
        write(\"2. Display Mother of?\\n\"),
        write(\"3. List all brothers of?\\n\"),
        write(\"4. List all sisters of?\\n\"),
        write(\"5. List all grandson of?\\n\"),
        write(\"6. List all granddaughter of?\\n\"),
        write(\"7. List all uncles of?\\n\"),
        write(\"8. List all aunty of?\\n\"),
        write(\"9. list all cousins of?\\n\"),
        write(\"0. exit\\n\"),
        write(\"Enter your choice : \"),
        readInt(Choice),
        action(Choice),
        write(\"\\n\"),
        repeat.

goal
    makewindow(1,2,3,\"Family Tree\",0,0,25,80),
    printmenu.


Output


+----------------------------------Family Tree--------------------------------+
¦                                                                             ¦
¦1. Display Father of?                                                        ¦
¦2. Display Mother of?                                                        ¦
¦3. List all brothers of?                                                     ¦
¦4. List all sisters of?                                                      ¦
¦5. List all grandson of?                                                     ¦
¦6. List all granddaughter of?                                                ¦
¦7. List all uncles of?                                                       ¦
¦8. List all aunty of?                                                        ¦
¦9. list all cousins of?                                                      ¦
¦0. exit                                                                      ¦
¦Enter your choice : 1                                                        ¦ 
¦                                                                             ¦
¦Enter name of person whose father is to be found : ram                       ¦
¦                                                                             ¦
¦Father of ram is:dashrath                                                    ¦
¦                                                                             ¦
¦1. Display Father of?                                                        ¦
¦2. Display Mother of?                                                        ¦
¦3. List all brothers of?                                                     ¦
¦4. List all sisters of?                                                      ¦
¦5. List all grandson of?                                                     ¦
¦6. List all granddaughter of?                                                ¦
¦7. List all uncles of?                                                       ¦
¦8. List all aunty of?                                                        ¦
¦9. list all cousins of?                                                      ¦
¦0. exit                                                                      ¦
¦Enter your choice : 3                                                        ¦
¦                                                                             ¦
¦Enter name of person whose brothers are to be found : ram                    ¦
¦                                                                             ¦
¦Brothers of ram are:                                                         ¦
¦laxman                                                                       ¦
¦bharat                                                                       ¦
¦                                                                             ¦
¦1. Display Father of?                                                        ¦
¦2. Display Mother of?                                                        ¦
¦3. List all brothers of?                                                     ¦
¦4. List all sisters of?                                                      ¦
¦5. List all grandson of?                                                     ¦
¦6. List all granddaughter of?                                                ¦
¦7. List all uncles of?                                                       ¦
¦8. List all aunty of?                                                        ¦
¦9. list all cousins of?                                                      ¦
¦0. exit                                                                      ¦
¦Enter your choice : 5                                                        ¦
¦                                                                             ¦
¦Enter name of person whose grandsons are to be found : dashrath              ¦
¦                                                                             ¦
¦Grandsons of dashrath are:                                                   ¦
¦luv                                                                          ¦
¦kush                                                                         ¦
¦son_of_laxman                                                                ¦
¦                                                                             ¦
¦1. Display Father of?                                                        ¦
¦2. Display Mother of?                                                        ¦
¦3. List all brothers of?                                                     ¦
¦4. List all sisters of?                                                      ¦
¦5. List all grandson of?                                                     ¦
¦6. List all granddaughter of?                                                ¦
¦7. List all uncles of?                                                       ¦
¦8. List all aunty of?                                                        ¦
¦9. list all cousins of?                                                      ¦
¦0. exit                                                                      ¦
¦Enter your choice : 7                                                        ¦
¦                                                                             ¦
¦Enter name of person whose uncles are to be found : kush                     ¦
¦                                                                             ¦
¦Uncles of kush are:                                                          ¦
¦laxman                                                                       ¦
¦bharat                                                                       ¦
¦                                                                             ¦
¦1. Display Father of?                                                        ¦
¦2. Display Mother of?                                                        ¦
¦3. List all brothers of?                                                     ¦
¦4. List all sisters of?                                                      ¦
¦5. List all grandson of?                                                     ¦
¦6. List all granddaughter of?                                                ¦
¦7. List all uncles of?                                                       ¦
¦8. List all aunty of?                                                        ¦
¦9. list all cousins of?                                                      ¦
¦0. exit                                                                      ¦
¦Enter your choice : 0                                                        ¦
¦                                                                             ¦
¦                                                                             ¦
¦Press the SPACE bar                                                          ¦
+-----------------------------------------------------------------------------+

    Related Post:
  1. Predicate logic representation and then converting them to prolog, prove the proof.

  2. ProLog Program to find length of names

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

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

  5. Prolog program to print the zodiac sign of a given birth date

  6. Prolog program to check whether a given list is palindrome or not

  7. PROLOG PROGRAM TO DETERMINE WHETHER A ELEMENT IS A MEMBER OF LIST

  8. Prolog program to insert an element anywhere in the list

  9. Prolog program to read file and create list of all the lines of it. Each list in turn should be list of word.

  10. PROLOG PROGRAM OF MEDICAL DIAGNOSIS SYSTEM OF CHILDHOOD DECEASE

  11. Program that defines a relation split(L,P,N) where list is divided into positive, negative numbers without using cut

  12. ProLog Program to find grade

  13. ProLog Program to sort a list using Quick Sort

  14. ProLog Program of asking question

  15. Prolog program of predicate ordered (List) which is true if List is an ordered list of numbers

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

  17. Prolog program to read a sentence from a file and then copy each of the token one by one into a list

  18. ProLog Program to find relations in family

  19. Prolog program to find summation of list of numbers

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

 
 
Didn't find what you were looking for? Find more on Prolog program for family hierarchy