Ling 388 language and computers
This presentation is the property of its rightful owner.
Sponsored Links
1 / 12

LING 388: Language and Computers PowerPoint PPT Presentation


  • 52 Views
  • Uploaded on
  • Presentation posted in: General

LING 388: Language and Computers. Sandiway Fong Lecture 7: 9/12. Administrivia. No class (this) Thursday Homework 2 due Thursday by midnight submit your homework by email. Today’s Topic. More practice with writing Prolog programs. Set: N = {1,2,3,4,5,..} Base case nn(1).

Download Presentation

LING 388: Language and Computers

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


Ling 388 language and computers

LING 388: Language and Computers

Sandiway Fong

Lecture 7: 9/12


Administrivia

Administrivia

  • No class (this) Thursday

  • Homework 2

    • due Thursday by midnight

    • submit your homework by email


Today s topic

Today’s Topic

  • More practice with writing Prolog programs


Natural numbers

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

Natural Numbers


Even numbers

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

Even Numbers


Even numbers1

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

Even Numbers


Even numbers2

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

Even Numbers


Even numbers3

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


Odd numbers

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

Odd Numbers


Output

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

Output


If then else

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

If-Then-Else


Nested if then else

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

Nested If-Then-Else


  • Login