slide1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Comparison PowerPoint Presentation
Download Presentation
Comparison

Loading in 2 Seconds...

play fullscreen
1 / 14

Comparison - PowerPoint PPT Presentation


  • 73 Views
  • Uploaded on

Comparison. X = Y True if X equals to Y. X \= Y True if X not equal to Y X < Y True if X is less than Y X > Y True if X is greater than Y

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Comparison' - deron


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1

Comparison

Lecture 15 Lists

slide2

X = Y True if X equals to Y

X \= Y True if X not equal to Y

X < Y True if X is less than Y

X > Y True if X is greater than Y

X =< Y True if X is less than or equal to Y

X >= Y True if X is greater than or equal to Y

Example:

Given two positive integers X and Y, their greatest common

devisor “D” is found according to the following:

if X = Y then D = X

if X < Y then D = greatest common devisor of X and Y – X

if X > Y then D = do the same as above with X and Y interchanged

greatest common devisor of 6 and 6 is 6

greatest common devisor of 27 and 9 is 9

greatest common devisor of 20 and 15 is 5

Lecture 15 Lists

slide3

gcd(X,X,X).

gcd(X,Y,D) :- X < Y,

Z is Y – X,

gcd(X, Z, D).

gcd(X,Y,D) :- Y < X,

gcd(Y, X, D).

We could have written the last clause above, as

gcd(X,Y,D) :- Y < X,

Z is X – Y,

gcd(Z, Y, D).

?- gcd(20,15,D).

D = 5

Yes

?- gcd(0,15,D).

ERROR: Out of local stack

Lecture 15 Lists

slide4

Another Example

n*(n-1)…2*1 n=1,2,3…

n!=

1 n=0

fact(0,1).

fact(N,F) :- N > 0,

N1 is N – 1,

fact(N1, F1),

F is N * F1.

?- edit(fact).

% d:/prolog/fact.pl compiled 0.00 sec, 64 bytes

Yes

?- fact(6,X).

X = 720

Yes

?- fact(3,X).

X = 6

Yes

Lecture 15 Lists

slide5

Lists

Lecture 15 Lists

slide6

The list is the main data structure in Prolog. A list may be empty:

[ ]

or it may contain one or more terms (constants, variables or structures).

[a,b,c,d,e]

[5,8,3,9,7]

[the, boy, run]

A list can be split into a head and a tail: [H|T].

grades(john, [70,87,90,58]).

?- grades(john, [H|T]).

H = 70

T = [87,90,58]

Lecture 15 Lists

slide7

Heads and tails of lists

List Head Tail

Lecture 15 Lists

slide8

?- [a, b, c] = [Head | Tail].

Head = a

Tail = [b, c]

?- [the,[boy,run]]=[Head|Tail].

Head = the

Tail = [[boy, run]]

?- [a, b, c] = [X, Y | Tail].

X = a

Y = b

Tail = [c]

?- [a, b, c] = [X|Y,Z].

No

Lecture 15 Lists

slide9

Converting a structure to a list

?- likes(john,mary)=..X.

X = [likes, john, mary]

Anything in doubleis equivalent to a list of ASCIIvalues:

?- "abc"=X.

X = [97, 98, 99]

Lecture 15 Lists

slide10

Recursion and Lists

member(X,[Y| _ ] ) :- X = Y.

member(X, [ _ | Y]) :- member(X, Y).

It would be easier to write this as:

member(X,[X| _ ]).

member(X, [ _ | Y]) :- member(X, Y).

?- member(1, [3,4,5,8,1,9]).

Yes

?- member(X, [prolog, c, ada, haskell]).

X= prolog;

X= c

X= ada;

X= haskell;

No

Lecture 15 Lists

slide11

Other Examples

change(you, i).

change(are, [am, not]).

change(french, australian).

change(do, no).

change(X, X). /* catchall */

alter([ ], [ ]).

alter([H|T], [X|Y]) :- change(H, X), alter(T,Y).

?- alter([you,are,french],R).

R = [i, [am, not], australian]

Yes

?- alter([you,are,a,computer],R).

R = [i, [am, not], a, computer]

Yes

?-

Lecture 15 Lists

slide12

asserta/1.

assertz/1

assert/1

retract/1

retractall/1

Example:

assertz(fib(N,F)).

Lecture 15 Lists

slide13

:-dynamic fib

fib(1,1).

fib(2,1).

fib(N,F) :-

N > 2,

N1 is N-1, fib(N1,F1),

N2 is N-2, fib(N2,F2),

F is F1 + F2,

asserta(fib(N,F)).

?- fib(8, F).

N = 21

?- fib(6,F).

F = 8

Lecture 15 Lists

slide14

+

f(5) f(4)

+

+

f(4) f(3)

f(3) f(2)

+

+

+

f(3)f(2)

f(2) f(1)

f(2) f(1)

1

+

1 1 1 1 1

f(2) f(1)

1 1

F(6)

Lecture 15 Lists