📄 http:^^www.cs.utexas.edu^users^dwip^cs304p^discussions^programming_in_prolog.html
字号:
MIME-Version: 1.0
Server: CERN/3.0
Date: Monday, 06-Jan-97 20:51:06 GMT
Content-Type: text/html
Content-Length: 3273
Last-Modified: Wednesday, 27-Nov-96 22:42:30 GMT
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><!--Converted with LaTeX2HTML 96.1 (Feb 5, 1996) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds --><HTML><HEAD><TITLE>Programming in Prolog</TITLE><META NAME="description" CONTENT="Programming in Prolog"><META NAME="keywords" CONTENT="prolog_handout"><META NAME="resource-type" CONTENT="document"><META NAME="distribution" CONTENT="global"><LINK REL=STYLESHEET HREF="prolog_handout.css"></HEAD><BODY LANG="EN"> <A NAME="tex2html12" HREF="how_to_run_prolog.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="/pub/icons/latex2html/next_motif.gif"></A> <BR> <B> Next:</B> <A NAME="tex2html13" HREF="how_to_run_prolog.html">How to run Prolog </A><BR> <P><H1><A NAME="SECTION00010000000000000000">Programming in Prolog</A></H1><P> Logic programming works by defining a set of facts and rules. Facts are simplest form of Prolog predicates. For example, suppose we want to express the fact that it is raining. In Prolog, it will be expressed as<P><PRE><TT> <code>raining.</code><P></TT></PRE><P>Whereas in Pascal, we will declare a boolean variable <code>raining</code>, and then assign it a value <B>true</B>.<P><PRE><TT> <B>var</B> ¯<P> <code>raining : </code> <B>boolean</B> <code>;</code><P> <P> <code>raining := </code> <B>true</B> <code>;</code><P></TT></PRE><P>Note the use of period (`.') after the statement. Each statement in Prolog must be ended by a period. Note all the predicates like <code>raining</code> must start withsmall letters. The words starting with capital letters are assumed to bevariables. A rule can be expressed using propositional logic taught inlecture. For example, the fact that `<EM>I'll get wet if it's raining</EM>', canbe expressed as<P><PRE><TT> <code>wet :- raining.</code><P></TT></PRE><P>and its equivalent code in Pascal will look like<P><PRE><TT> <code>if </code>¯<code>( raining ) then</code><P> <code>wet := </code> <B>true</B> <code>;</code><P></TT></PRE><P>The symbol `:-' is read as <EM>if</EM>. The <EM>and</EM> and <EM>or</EM>operators can also be expressed in Prolog easily. For example,<P><PRE><TT> <code>miserable :- cold, wet.</code><P></TT></PRE><P>means that `<EM>I'll be miserable if it's cold <B>and</B> if I get wet</EM>'. Thesymbol `,' is read as <EM>and</EM>. Similarly,<P><PRE><TT> <code>miserable :- cold.</code><P><code>miserable :- wet.</code><P></TT></PRE><P>expresses the fact that '<EM>I'll be miserable if it's cold <B>or</B> if I get wet</EM>'.<P><DL COMPACT><DT><STRONG>1)</STRONG><DD> Write prolog statements for the following<DT><STRONG></STRONG><DD><OL><LI> I am mortal.<LI> A metal is gold if it is yellow and heavy.<LI> I will pass the course if I do well in the finals.</OL> </DL><P> Comments can we written in Prolog by putting a percent sign (%) at the beginning of a line.<P><PRE><TT> <code>% This is an example program</code><P><code>% It illustrates how to put comments</code><P><code>wet :- raining.</code><P><code>miserable :- cold.</code><P><code>miserable :- wet.</code><P></TT></PRE><BR> <HR><P><ADDRESS><I>Dwip Narayan Banerjee <BR>Wed Nov 27 16:24:30 CST 1996</I></ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -