Prolog Tutorial

 
 
 

Prolog Basics

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 Print zodiac sign of a given birth date 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) 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) Find sum of all numbers in given list 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 Rotate list n places to the left

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 Find last element in a given list Read file and create a list of all words of it 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 Split list in negative & positive list 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 Concatenate two lists in giving third list Copy in database - string length equal to number 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 Person hobbies 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) Add element in last position in given list Predicate logic representation and prove proof 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 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

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 Check number is member of given list or not 10 int in list,10 from database - find 2nd max 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 Logic repres - converting to prove the proof 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 Delete an element from given list Copy String length equal to number using file 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 Find last item of the list 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 number from 1 to 25 Murder mystery
 
 
%trace
database-book
    book_record(integer,string,integer)

database-copy
    copy_record(integer,integer,integer)
    issued_copies(integer)    

predicates
    insert_details
    get_book_details(integer,string,integer)
    find_issued_copies(integer,integer)
    increment_issued_count
    find_available_copies(integer)

clauses

    insert_details:-
        retractall(book_record(_,_,_)),
        retractall(copy_record(_,_,_)),
        assert(book_record(1,ai,6)),
        assert(book_record(2,nt,5)),
        assert(book_record(3,ooad,2)),
        assert(copy_record(1,1,1)),
        assert(copy_record(1,2,2)),
        assert(copy_record(1,3,3)),
        assert(copy_record(3,1,1)).

    get_book_details(BookNo,BookName,TotalCopies):-
        book_record(BookNo,BookName,TotalCopies).

    find_issued_copies(BookNo,IssuedCopies):-
        retractall(issued_copies(_)),
        assert(issued_copies(0)),
        copy_record(BookNo,_,_),
        increment_issued_count,
        fail.

    find_issued_copies(BookNo,IssuedCopies):-
        issued_copies(IssuedCopies),
        retractall(issued_copies(_)).

    increment_issued_count:-
        issued_copies(Count),
        NewCount = Count + 1,
        retract(issued_copies(Count)),
        assert(issued_copies(NewCount)),
        !.

    find_available_copies(BookNo):-
        get_book_details(BookNo,BookName,TotalCopies),
        find_issued_copies(BookNo,IssuedCount),
        AvailableCount = TotalCopies-IssuedCount,
        write(\"[\",AvailableCount,\"] copies are available of \",BookName),
        nl.

goal
    clearwindow,
    makewindow(1,2,3,\"Book Copy Availibility\",0,0,25,80),
    insert_details,
    find_available_copies(1),
    find_available_copies(2),
    find_available_copies(3).


Output

+----------------------------Book Copy Availibility---------------------------+
¦[3] copies are available of ai                                               ¦
¦[5] copies are available of nt                                               ¦
¦[1] copies are available of ooad                                             ¦
¦                                                                             ¦
¦Press the SPACE bar                                                          ¦
¦                                                                             ¦
¦                                                                             ¦
+-----------------------------------------------------------------------------+

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

  2. Prolog program to check whether a given word is a palindrome or not

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

  4. Prolog program to concatenate two lists giving third list

  5. Prolog program to find whether the length of a list is even or odd

  6. PROLOG PROGRAM OF MISSIONARY CANNIBAL PROBLEM

  7. Prolog program to read 10 string and store them in database and then write the string in increasing order of their length to other database

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

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

  10. ProLog Program to search record by name and phone number

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

  12. Prolog program to reverse a list

  13. ProLog Program to sort the list of integers using Insertion Sort

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

  15. Program to find easy courses person likes

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

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

  18. Prolog program to find the permutations of a given list i.e. to find all possible combinations of the elements of a list

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

  20. Prolog program to read number