Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Like this presentation? Why not share!

- Prolog: Arithmetic Operations In Pr... by PROLOG CONTENT 1986 views
- Prolog programming by Young Alista 44 views
- PROLOG: Fact Roles And Queries In P... by PROLOG CONTENT 6539 views
- PROLOG: Cuts And Negation In Prolog by DataminingTools Inc 11162 views
- Prolog Programming : Basics by Mitul Desai 2302 views
- Arithmetic and constructing express... by Tamer Abu-alzenat 257 views

12,432 views

Published on

PROLOG: Arithmetic Operations In Prolog

Published in:
Technology

No Downloads

Total views

12,432

On SlideShare

0

From Embeds

0

Number of Embeds

59

Shares

0

Downloads

0

Comments

0

Likes

1

No embeds

No notes for slide

- 1. Arithmetic operations in Prolog<br />
- 2. OVERVIEW<br />Arithmetic in Prolog<br />Arithmetic and Lists<br />Comparing integers<br />Examples<br />
- 3. Arithmetic in Prolog<br />Prolog provides a number of basic arithmetic tools for manipulating integers (that is, numbers of the form ...-3, -2, -1, 0, 1, 2, 3, 4...).<br />Integers are useful for various tasks (such as finding the length of a list)<br />Lets look at how Prolog handles the four basic operations of addition, multiplication, subtraction, and division.<br />
- 4. Arithmetic in Prolog<br />
- 5. Ex arithmetic queries:<br />?- 8 is 6+2.<br /> yes<br />?- 12 is 6*2.<br /> yes<br />?- -2 is 6-8.<br /><ul><li>Yes</li></ul>?- add_3_and_double(1,X).<br />X = 8<br />?- add_3_and_double(2,X).<br />X = 10<br />?- X is 3+2*4.<br />X = 11<br />
- 6. Arithmetic and Lists<br />Length of the list is recursive defined as:<br />1. The empty list has length zero.<br />2. A non-empty list has length 1 + len(T), where len(T) is the length of its tail.<br />This definition is practically a Prolog program already. Here's the code we need:<br />len([],0).<br />len([_|T],N) :- len(T,X), N is X+1.<br />On posing the query:<br />?- len([a,b,c,d,e,[a,b],g],X).<br />we get X = 7<br />
- 7. We can use an accumulator to calculate the length of a list.<br />We shall define a predicate accLen3/ which takes the following arguments.<br />accLen(List,Acc,Length)<br />Here List is the list whose length we want to find, and Length is its length (an integer).<br />Acc is a variable we will use to keep track of intermediate values for length(so it will also be an integer).<br />We can define a predicate which calls accLen for us, and gives it the initial value of 0:<br />leng(List,Length) :- accLen(List,0,Length).<br />So now we can pose queries like this:<br />leng([a,b,c,d,e,[a,b],g],X).<br />
- 8. Comparing Integers<br />Operators that compare integers are:<br />
- 9. Examples:<br />2 < 4.<br />yes<br />2 =< 4.<br />yes<br />4 =< 4.<br />yes<br />4=:=4.<br />yes<br />4==5.<br />yes<br />4==4.<br />no<br />4 >= 4.<br />yes<br />
- 10. Examples<br />2+1 < 4.<br />yes<br />2+1 < 3+2.<br />yes<br />Note that =:= really is different from =, as the following examples show:<br />4=4.<br />yes<br />2+2 =4.<br />no<br />2+2 =:= 4.<br /> yes<br />
- 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.<br />We can use an accumulator. <br />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. <br />Here's the code. Note that there are two recursive clauses:<br />accMax([H|T],A,Max) :-<br />H > A,<br />accMax(T,H,Max).<br />accMax([H|T],A,Max) :-<br />H =< A,<br />accMax(T,A,Max).<br />accMax([],A,A).<br />
- 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.<br />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. <br />Finally, the base clause unifies the second and third arguments; it gives the highest value we found<br />
- 13. while going through the list to the last argument. Here's how it works:<br />accMax([1,0,5,4],0,_5810)<br />accMax([0,5,4],1,_5810)<br />accMax([5,4],1,_5810)<br />accMax([4],5,_5810)<br />accMax([],5,_5810)<br />accMax([],5,5)<br />suppose we give a list of negative integers as input. Then we would have<br />accMax([-11,-2,-7,-4,-12],0,Max).<br />Max = 0<br /> yes<br />
- 14. Visit more self help tutorials<br />Pick a tutorial of your choice and browse through it at your own pace.<br />The tutorials section is free, self-guiding and will not involve any additional support.<br />Visit us at www.dataminingtools.net<br />

No public clipboards found for this slide

×
### Save the most important slides with Clipping

Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.

Be the first to comment