SlideShare a Scribd company logo
Arithmetic operations  in Prolog
OVERVIEWArithmetic in PrologArithmetic and ListsComparing integersExamples
Arithmetic in PrologProlog provides a number of basic arithmetic tools for manipulating integers (that is, numbers of the form ...-3, -2, -1, 0, 1, 2, 3, 4...).Integers are useful for various tasks (such as finding the length of a list)Lets look at how Prolog handles the four basic operations of addition, multiplication, subtraction, and division.
Arithmetic in Prolog
Ex arithmetic queries:?- 8 is 6+2. yes?- 12 is 6*2. yes?- -2 is 6-8.Yes?- add_3_and_double(1,X).X = 8?- add_3_and_double(2,X).X = 10?- X is 3+2*4.X = 11
Arithmetic and ListsLength of the list is  recursive defined as:1. The empty list has length zero.2. A non-empty list has length 1 + len(T), where len(T) is the length of its tail.This definition is practically a Prolog program already. Here's the code we need:len([],0).len([_|T],N) :- len(T,X), N is X+1.On posing the query:?- len([a,b,c,d,e,[a,b],g],X).we get X = 7
We can use an accumulator to calculate the length of a list.We shall define a predicate accLen3/ which takes the following arguments.accLen(List,Acc,Length)Here List is the list whose length we want to find, and Length is its length (an integer).Acc is a variable we will use to keep track of intermediate values for length(so it will also be an integer).We can define a predicate which calls accLen for us, and gives it the initial value of 0:leng(List,Length) :- accLen(List,0,Length).So now we can pose queries like this:leng([a,b,c,d,e,[a,b],g],X).
Comparing IntegersOperators that compare integers are:
Examples:2 < 4.yes2 =< 4.yes4 =< 4.yes4=:=4.yes4=\=5.yes4=\=4.no4 >= 4.yes
Examples2+1 < 4.yes2+1 < 3+2.yesNote that =:= really is different from =, as the following examples show:4=4.yes2+2 =4.no2+2 =:= 4. yes
We can define a predicate which takes a list of non-negative integers as its first argument, and returns the maximum integer in the list as its last argument.We can  use an accumulator. As we work our way down the list, the accumulator will keep track of the highest integer found so far. If we find a higher value, the accumulator will be updated to this new value. When we call the program, we set accumulator to an initial value of 0. Here's the code. Note that there are two recursive clauses:accMax([H|T],A,Max) :-H > A,accMax(T,H,Max).accMax([H|T],A,Max) :-H =< A,accMax(T,A,Max).accMax([],A,A).
The first clause tests if the head of the list is larger than the largest value found so far. If it is, we set the accumulator to this new value, and then recursively work through the tail of the list.The second clause applies when the head is less than or equal to the accumulator; in this case we recursively work through the tail of the list using the old accumulator value. Finally, the base clause unifies the second and third arguments; it gives the highest value we found
while going through the list to the last argument. Here's how it works:accMax([1,0,5,4],0,_5810)accMax([0,5,4],1,_5810)accMax([5,4],1,_5810)accMax([4],5,_5810)accMax([],5,_5810)accMax([],5,5)suppose we give a list of negative integers as input. Then we would haveaccMax([-11,-2,-7,-4,-12],0,Max).Max = 0      yes
Visit more self help tutorialsPick a tutorial of your choice and browse through it at your own pace.The tutorials section is free, self-guiding and will not involve any additional support.Visit us at www.dataminingtools.net

More Related Content

PPTX
PROLOG: Arithmetic Operations In Prolog
PPTX
PROLOG: Matching And Proof Search In Prolog
PPT
Prolog programming
PDF
Anton Kasyanov, Introduction to Python, Lecture4
PPTX
Binary search
PDF
haskell_fp1
PDF
Binary Search - Design & Analysis of Algorithms
PDF
Application of hashing in better alg design tanmay
PROLOG: Arithmetic Operations In Prolog
PROLOG: Matching And Proof Search In Prolog
Prolog programming
Anton Kasyanov, Introduction to Python, Lecture4
Binary search
haskell_fp1
Binary Search - Design & Analysis of Algorithms
Application of hashing in better alg design tanmay

What's hot (20)

PPT
Data Structure In C#
PPT
Data Structure and Algorithms Hashing
PPT
list procedures
PPTX
Hashing Technique In Data Structures
PPTX
Python dictionary
PPT
Data structure lecture 2
PPTX
Arrays in Data Structure and Algorithm
PPS
Ds 8
PDF
08 Hash Tables
PPTX
Dictionaries
PPTX
Hashing
PPTX
linear probing
PPT
Ch17 Hashing
PPT
Hashing
PPTX
Coin Changing, Binary Search , Linear Search - Algorithm
PPTX
Hashing 1
PPTX
Data structures in c#
PPTX
Quadratic probing
PDF
Concurrent Hashing and Natural Parallelism : The Art of Multiprocessor Progra...
ZIP
Hashing
Data Structure In C#
Data Structure and Algorithms Hashing
list procedures
Hashing Technique In Data Structures
Python dictionary
Data structure lecture 2
Arrays in Data Structure and Algorithm
Ds 8
08 Hash Tables
Dictionaries
Hashing
linear probing
Ch17 Hashing
Hashing
Coin Changing, Binary Search , Linear Search - Algorithm
Hashing 1
Data structures in c#
Quadratic probing
Concurrent Hashing and Natural Parallelism : The Art of Multiprocessor Progra...
Hashing
Ad

Viewers also liked (14)

PPT
Prolog basics
PDF
"That scripting language called Prolog"
PPT
Artificial intelligence Prolog Language
PPTX
PROLOG: Fact Roles And Queries In Prolog
PPTX
ProLog (Artificial Intelligence) Introduction
PPTX
PROLOG: Database Manipulation In Prolog
PPTX
PROLOG: Database Manipulation In Prolog
PPTX
Prolog: Cuts And Negation In Prolog
TXT
Prolog Code [Family Tree] by Shahzeb Pirzada
PDF
Logic programming (1)
PPTX
Introduction on Prolog - Programming in Logic
PPTX
Prolog 7-Languages
PPTX
PROLOG: Cuts And Negation In Prolog
PPTX
Prolog Programming : Basics
Prolog basics
"That scripting language called Prolog"
Artificial intelligence Prolog Language
PROLOG: Fact Roles And Queries In Prolog
ProLog (Artificial Intelligence) Introduction
PROLOG: Database Manipulation In Prolog
PROLOG: Database Manipulation In Prolog
Prolog: Cuts And Negation In Prolog
Prolog Code [Family Tree] by Shahzeb Pirzada
Logic programming (1)
Introduction on Prolog - Programming in Logic
Prolog 7-Languages
PROLOG: Cuts And Negation In Prolog
Prolog Programming : Basics
Ad

Similar to Prolog: Arithmetic Operations In Prolog (20)

PDF
Homework Assignment – Array Technical DocumentWrite a technical .pdf
PDF
Unit ii data structure-converted
PDF
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
PPT
9781439035665 ppt ch09
PPTX
Lecture 7
PDF
advanced python for those who have beginner level experience with python
PDF
Advanced Python after beginner python for intermediate learners
PPT
Comp102 lec 8
PDF
DS unit 10000000000000000000000000000.pdf
PPTX
Algorithm, Concepts in performance analysis
PPT
Programming in Python Lists and its methods .ppt
PPT
Chap09
PPT
Python - Data Collection
PPT
Arrays and vectors in Data Structure.ppt
PPTX
Chapter-Five.pptx
PDF
Java arrays (1)
PDF
Sortsearch
PDF
Arrays and library functions
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Unit ii data structure-converted
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
9781439035665 ppt ch09
Lecture 7
advanced python for those who have beginner level experience with python
Advanced Python after beginner python for intermediate learners
Comp102 lec 8
DS unit 10000000000000000000000000000.pdf
Algorithm, Concepts in performance analysis
Programming in Python Lists and its methods .ppt
Chap09
Python - Data Collection
Arrays and vectors in Data Structure.ppt
Chapter-Five.pptx
Java arrays (1)
Sortsearch
Arrays and library functions

Recently uploaded (20)

PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Empathic Computing: Creating Shared Understanding
PPT
Teaching material agriculture food technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
cuic standard and advanced reporting.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
KodekX | Application Modernization Development
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Empathic Computing: Creating Shared Understanding
Teaching material agriculture food technology
Reach Out and Touch Someone: Haptics and Empathic Computing
cuic standard and advanced reporting.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Spectral efficient network and resource selection model in 5G networks
Understanding_Digital_Forensics_Presentation.pptx
NewMind AI Weekly Chronicles - August'25 Week I
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
The AUB Centre for AI in Media Proposal.docx
Review of recent advances in non-invasive hemoglobin estimation
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Encapsulation_ Review paper, used for researhc scholars
Chapter 3 Spatial Domain Image Processing.pdf
KodekX | Application Modernization Development
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...

Prolog: Arithmetic Operations In Prolog

  • 2. OVERVIEWArithmetic in PrologArithmetic and ListsComparing integersExamples
  • 3. Arithmetic in PrologProlog provides a number of basic arithmetic tools for manipulating integers (that is, numbers of the form ...-3, -2, -1, 0, 1, 2, 3, 4...).Integers are useful for various tasks (such as finding the length of a list)Lets look at how Prolog handles the four basic operations of addition, multiplication, subtraction, and division.
  • 5. Ex arithmetic queries:?- 8 is 6+2. yes?- 12 is 6*2. yes?- -2 is 6-8.Yes?- add_3_and_double(1,X).X = 8?- add_3_and_double(2,X).X = 10?- X is 3+2*4.X = 11
  • 6. Arithmetic and ListsLength of the list is recursive defined as:1. The empty list has length zero.2. A non-empty list has length 1 + len(T), where len(T) is the length of its tail.This definition is practically a Prolog program already. Here's the code we need:len([],0).len([_|T],N) :- len(T,X), N is X+1.On posing the query:?- len([a,b,c,d,e,[a,b],g],X).we get X = 7
  • 7. We can use an accumulator to calculate the length of a list.We shall define a predicate accLen3/ which takes the following arguments.accLen(List,Acc,Length)Here List is the list whose length we want to find, and Length is its length (an integer).Acc is a variable we will use to keep track of intermediate values for length(so it will also be an integer).We can define a predicate which calls accLen for us, and gives it the initial value of 0:leng(List,Length) :- accLen(List,0,Length).So now we can pose queries like this:leng([a,b,c,d,e,[a,b],g],X).
  • 8. Comparing IntegersOperators that compare integers are:
  • 9. Examples:2 < 4.yes2 =< 4.yes4 =< 4.yes4=:=4.yes4=\=5.yes4=\=4.no4 >= 4.yes
  • 10. Examples2+1 < 4.yes2+1 < 3+2.yesNote that =:= really is different from =, as the following examples show:4=4.yes2+2 =4.no2+2 =:= 4. yes
  • 11. We can define a predicate which takes a list of non-negative integers as its first argument, and returns the maximum integer in the list as its last argument.We can use an accumulator. As we work our way down the list, the accumulator will keep track of the highest integer found so far. If we find a higher value, the accumulator will be updated to this new value. When we call the program, we set accumulator to an initial value of 0. Here's the code. Note that there are two recursive clauses:accMax([H|T],A,Max) :-H > A,accMax(T,H,Max).accMax([H|T],A,Max) :-H =< A,accMax(T,A,Max).accMax([],A,A).
  • 12. The first clause tests if the head of the list is larger than the largest value found so far. If it is, we set the accumulator to this new value, and then recursively work through the tail of the list.The second clause applies when the head is less than or equal to the accumulator; in this case we recursively work through the tail of the list using the old accumulator value. Finally, the base clause unifies the second and third arguments; it gives the highest value we found
  • 13. while going through the list to the last argument. Here's how it works:accMax([1,0,5,4],0,_5810)accMax([0,5,4],1,_5810)accMax([5,4],1,_5810)accMax([4],5,_5810)accMax([],5,_5810)accMax([],5,5)suppose we give a list of negative integers as input. Then we would haveaccMax([-11,-2,-7,-4,-12],0,Max).Max = 0 yes
  • 14. Visit more self help tutorialsPick a tutorial of your choice and browse through it at your own pace.The tutorials section is free, self-guiding and will not involve any additional support.Visit us at www.dataminingtools.net