Loading in 5 sec....

LING 388: Language and ComputersPowerPoint Presentation

LING 388: Language and Computers

- 60 Views
- Uploaded on
- Presentation posted in: General

LING 388: Language and Computers

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 - - - - - - - - - - - - - - - - - - - - - - - - - -

LING 388: Language and Computers

Sandiway Fong

Lecture 7: 9/12

- No class (this) Thursday
- Homework 2
- due Thursday by midnight
- submit your homework by email

- More practice with writing Prolog programs

Set:

N = {1,2,3,4,5,..}

Base case

nn(1).

“1 is a natural number”

Recursive case

nn(N) :- nn(M), N is M+1.

“N is a natural number if M is a natural number and N is M+1”

?- nn(X).

X = 1 ;

X = 2 ;

X = 3 ;

X = 4 ;

X = 5 ;

X = 6 ;

X = 7 ;

X = 8 ;

X = 9

Definition:

even numbers are natural numbers that are divisible by 2

Set:

N = {1,2,3,4,5,..}

E = {2,4,..}

Task:

Write a predicate even/1 that generates the even numbers

Method 1

Base Case

even(2).

Recursive Case

even(N) :- even(M), N is M+2.

Query:

?- e(X).

X = 2 ;

X = 4 ;

X = 6 ;

X = 8

Definition:

even numbers are natural numbers that are divisible by 2

Set:

N = {1,2,3,4,5,..}

E = {2,4,..}

Task:

Write a predicate even/1 that generates the even numbers

Method 2

Use the natural number definition and permit only the even ones

Modify:

nn(1).

nn(N) :- nn(M), N is M+1.

into

even(N) :- nn(N), is_even(N).

where is_even(N) tests N to see if it’s an even number

Method 2

Use the natural number definition and permit only the even ones

Modify:

nn(1).

nn(N) :- nn(M), N is M+1.

into

even(N) :- nn(N), is_even(N).

where is_even(N) tests N to see if it’s an even number

Definition:

even numbers are natural numbers that are divisible by 2

Let’s use Prolog’s built-in arithmetic evaluation predicate is/2

?- Y is 3/2.

produces a floating point number

Y=1.5

?- Y is 4/2.

produces an integer

Y=2

?- Y is round(3/2).

rounds up to the nearest integer

Y=2

Definition:

even numbers are natural numbers that are divisible by 2

Let’s use Prolog’s built-in arithmetic evaluation predicate is/2

?- Y is 3/2.

produces a floating point number

Y=1.5

?- Y is 4/2.

produces an integer

Y=2

?- Y is round(3/2).

rounds up to the nearest integer

Y=2

Definition so far

even(N) :- nn(N), is_even(N).

where is_even(N) tests N to see if it’s an even numberIdea:

to test whether a number X is even

divide it by 2, call this number Y

divide it by 2 and round it, call this number Z

X is even if Y=Z.

Define

is_even(X) :-

Y is X/2,

Z is round(X/2),

Y = Z.

Even numbers

Idea:

to test whether a number X is even

divide it by 2, call this number Y

divide it by 2 and round it, call this number Z

X is even if Y=Z.

Define

is_even(X) :-

Y is X/2,

Z is round(X/2),

Y = Z.

Odd numbers

Define

is_odd(X) :-

Y is X/2,

Z is round(X/2),

\+ Y = Z.

or define

is_odd(X) :- \+ is_even(X).

You can use built-in predicates

write/1 and

nl/1

to print values of variables etc.

Example

if X = the

?- write(the),nl.

prints

the

[newline]

Sometimes useful for debugging

is_odd(X) :-

Y is X/2,

write(Y), nl,

Z is round(X/2),

write(Z), nl,

\+ Y = Z.

Examples:

?- is_odd(7).

3.5

4

Yes

?- is_odd(6).

3

3

No

Template:

<test>

-> <then-part>

; <else-part>

<test>

<then-part>

<else-part>

are all Prolog goals (queries)

Example:

test(N) :-

is_even(N)

-> write(N),

write(‘ is even’),

nl

; write(N),

write(‘ is odd’),

nl.

Queries:

?- test(3).

3 is odd

Yes

?- test(4).

4 is even

Yes

Template:

<test>

-> <then-part>

; (<test>

-> <then-part>

; <else-part>)

<test>

<then-part>

<else-part>

are all Prolog goals (queries)

( ... )

indicate nesting

Example:

test(N) :-

N = 0

-> write(‘Zero’), nl

; (is_even(N)

-> write(N),

write(‘ is even’),

nl

; write(N),

write(‘ is odd’),

nl).

Queries:

?- test(0).

Zero