CS 4303 Programming Language Concepts



CS 4303 Programming Language Concepts

Lab – Chapter 4 Logic Programming

The purpose of this lab is to learn basic Prolog programming.

Activity 1. Install Prolog software SWI Prolog: Go to , click “download” link on the left pane, choose “stable release” for MS Windows, on the list of the versions, choose the one for the computer you are using, install the software.

Activity 2. Get acquainted with SWI Prolog’s file editing and activation features. Learn how to edit and save Prolog program (follow “File -> Edit” path) and how to load the software into the runtime system (use “File -> Consult”).

Activity 3. Write the following statements as Prolog clauses, in the order given:

If it is raining or snowing, then there is precipitation.

If it is freezing and there is precipitation, then it is snowing.

If it is not freezing and there is precipitation, then it is raining.

It is snowing.

What answer does Prolog give to the following queries:

Is it freezing?

Is it raining?

Can you rearrange the clauses so that Prolog can give better answers?

Activity 4. Write the following statement as Prolog clauses:

Mammals have four legs and no arms, or two arms and two legs.

A horse is a mammal.

A horse has no arms.

Can Prolog derive that a horse has four legs?

Activity 5. Write Prolog clauses to express the following relationship, given the parent relationship: grandparent, sibling, cousin. Enter the following family relationship as Prolog clauses.

[pic]

Use queries to find out:

Who is Ana’s grandparent?

Who is Jill’s sibling?

Who is Sarah’s cousin?

If multiple answers exist, make sure to get all of them.

Activity 6. Write a Prolog definition of the greatest common divisor of two numbers. Then use it to compute gcd(4, 10), gcd(15, 36), and gcd(25, 55).

Activity 7. Write a Prolog program to find the last item in a list.

Turn in a Prolog program file in responses to Activities 3-7 in Blackboard Vista by the due day.

-----------------------

Bill

Jill

Sarah

Sam

Ana

Jan

Smith

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download