酷兔英语

章节正文
文章总共2页


truncate(Exp)

remove fractional part of Exp: truncate(–1.5) = –1, truncate(1.5) = 1



round(Exp)

round Exp to nearest integer: round(1.6) = 2, round(1.3) = 1



ceiling(Exp)

smallest integer ≥ Exp: ceiling(1.3) = 2




These functions should be used in a context where they will actually be evaluated, such as following is or as part of an arithmeticcomparisonoperator like =:= or >.



Example:


?- X is sqrt(2).
X = 1.41421
Compare this with the following, where sqrt(2) is not evaluated, because = does not evaluate its arguments.
?- X = sqrt(2).
X = sqrt(2)


 


Another example:
?- X is log(3+2).
X = 1.60944.


These mathematical functions may correspond to arity 2 built-in predicates: for example, one can do this:


?- sqrt(2, X).
X = 1.41421
Some versions of SWI Prolog (e.g. 5.6.47) implement many of these arity 2 predicates, but not e.g. exp/2.


 



* High School Maths Reminder Service: if you want the logarithm to base a, divide log(Exp) by log(a). E.g. log10(X) = log(X)/log(10), and log2(X) = log(X)/log(2).





 



 



call

call is a built-in meta-predicate that allows its single argument to be called/invoked as a goal. For example, a program might create a goal (perhaps using =..) on the fly, and then, presumably later in the program, need to test the goal. Here are queries that perform these roles - in a real program, both the assert and the call would be built in to Prolog procedures written by the programmer.
?- assert(likes(mary, pizza)).

?- call(likes(Person, pizza)).
Person = mary

?- Goal =.. [likes, mary, What], call(Goal).
Goal = likes(mary, pizza)
What = pizza


 




 




clause


A clause in Prolog is a unit of information in a Prolog programending with a full stop ("."). A clause may be a fact, like:



 



likes(mary, pizza).
food(pizza).


or a rule, like:



eats(Person, Thing) :- likes(Person, Thing), food(Thing).


 




A clause may also be a query to the Prolog interpreter, as in:



?- eats(mary, pizza).


A group of clauses about the same relation is termed a procedure.




 




commenting your code


In Prolog, a the start of a comment is signalled by the character%. Comments are ignored by the Prolog interpreter; their purpose is to help a human reader understand the Prolog code. Examples:


 


% This is a full line comment.
% The next line contains a part-line comment.
member(Item, [Item|Rest]). % member succeeds if Item is 1st object in list.


Commenting Guidelines



As in other programming languages, comments should be used freely to explain the high-level significance of sections of code, to explain tricky sections of code, etc. Comments should not echo what the code already clearly says. Comments like that actually get in the way of understanding, for example because they are likely to make the reader ignore the comments. Where it is possible to make code understandable by using a meaningful functor name or variable name, this is preferable to a comment.



It is good practice to begin each Prolog procedure with a comment describing what it does, e.g.


% member(Item, List) - succeeds if the item is a member of the list


If the list needs to have some special properties, e.g. if it must be a list of numbers, or must be instantiated (that is, have a value) at the time the procedure is called, then this headercomment should say so:


% member(Item, List) - succeeds if the item is a member of the list;
% List must be instantiated at the time member is called. Item need not
% be instantiated.


 



It can be a good idea for the header comments to indicate examples of the kind of data on which the procedure is intended to operate, and what the result should be, if this can be done reasonably briefly. E.g.


% Examples of use:
% ?- member(b, [a, b, c]).
% true.
%
% ?- member(X, [a, b, c]).
% X = a ;
% X = b ;
% X = c ;
% false.


 



Each file of Prolog code should begin with a (file) header comment, indicating who wrote the code, when, and what it is (overall) intended to do. This would be enough in a short Prolog assignment. In a "industrial strength" Prolog system, there would be details on the origins of algorithms used, and a revision history.



A good source of examples of Prolog commenting is example code made available in class, such as this one. Note however that this one is rather more heavily commented than usual, for instructional purposes. Note also that code examples presented on screen may be under-commented, because of the difficulty of fitting the comments and the code on the screen, and because the oral presentation accompanying the on-screen material replaces the comments to some extent.



Don't make your lines of comments (or code) too long - long lines can be hard to read, and really long lines may be folded, turning your neat formatting into a dog's breakfast. Stick to a maximum line length of 70 or 80 characters. Cute lines and/or boxes constructed of comment characters have the side effect of preventing the reader from seeing as much of your code at one time. The reader may then have to page up and down to figure out what your code does. They will not like you for this.



The use of cuts should normally be commented to explain why the cut is necessary.



It is a bad idea to comment (almost) every line, partly because of the clutter, and the distraction that this causes, and partly because most such comments are pointless duplications of the code (and/or comment things obvious except to a novice Prolog programmer):



Example of bad commenting (every line commented):


factorial(0, 1).                     % Factorial of 0 is 1.
factorial(N, FactN) :-
N > 0, % N is positive
Nminus1 is N - 1, % Calculate N minus 1
factorial(Nminus1, FactNminus1), % recursion
FactN is N * FactNminus1. % N! = N * (N - 1)!
Of these comments, only the first and last are even faintly justifiable, and even these are probably too obvious, as, particularly for the last comment, the variable names tell you everything you need to know. It looks pretty, with all the %-signs neatly lined up, but it forces the reader to check through all the unnecessary comments in case there is anything important there. This code needs a procedure header comment, and probably nothing else, though a beginning Prolog programmer might be justified in pointing out that the line N > 0, is there to make sure that the rule is only used in the case where N is positive. (If you're not sure why this is so important, try leaving out N > 0, and test the function on say N = 2.) So the comment would be "% only use rule if N > 0."


How to write even worse comments!



It's easy - just write comments that are actually wrong. sad face

Review your comments in a cool moment, and make sure that what they say is true.



The + - ? convention for arguments to procedures



A convention often used to make the role of arguments to a procedure clearer is to tag them with +, , and ?, as follows:






















Tag

Meaning

+

argument is expected to be instantiated (i.e. have a value rather than be a variable) when the procedure is called as a goal.


argument is expected to be a variable to be bound when the procedure is called as a goal.

?

argument may be either instantiated, or be an unbound variable, when the procedure is called as a goal.


Example:


% factorial(+N, -FactorialN).
%% supply a value for N, and FactorialN will be computed.

% member(?Item, ?List).
%% Item and List may either be instantiated, or a variable
%% (but in fact at least one of them must be instantiated!)
It's worth checking out, by the way, what happens if List is not instantiated …
?- member(a, List).

List = [a|_G231] ;

List = [_G230, a|_G234] ;

List = [_G230, _G233, a|_G237]
and so on … Prolog works its way through all list structures that containa.


 



Spelling, grammar, etc.



It's no bad thing if all of these comments are spelled correctly and are grammatically phrased. (Sometimes it is appropriate to abbreviate, so perhaps your comments don't all need to be full sentences.) It makes life harder for folks trying to read your code, if they have to wade through poor spelling and grammar. Remember that the people reading your code will include people you will want to understand your comments easily - helpers, markers, colleagues, your boss, even yourself in a year's time when you've forgotten how you got the code to work … So get into good commenting habits from the start.



See also indentation and white space. 




  • interpreter [in´tə:pritə] 移动到这儿单词发声  n.译员;解释者;翻译器   (初中英语单词)
  • solution [sə´lu:ʃən] 移动到这儿单词发声  n.解答;解决;溶解   (初中英语单词)
  • otherwise [´ʌðəwaiz] 移动到这儿单词发声  ad.另外 conj.否则   (初中英语单词)
  • suitable [´su:təbəl, ´sju:-] 移动到这儿单词发声  a.合适的,适当的   (初中英语单词)
  • desirable [di´zaiərəbəl] 移动到这儿单词发声  a.向往的;极好的   (初中英语单词)
  • achieve [ə´tʃi:v] 移动到这儿单词发声  vt.完成;达到;获得   (初中英语单词)
  • application [,æpli´keiʃən] 移动到这儿单词发声  n.申请;申请书;应用   (初中英语单词)
  • properly [´prɔpəli] 移动到这儿单词发声  ad.适当地;严格地   (初中英语单词)
  • output [´autput] 移动到这儿单词发声  n.产品;产品;计算结果   (初中英语单词)
  • artificial [,ɑ:ti´fiʃəl] 移动到这儿单词发声  a.人工的;模拟的   (初中英语单词)
  • intelligence [in´telidʒəns] 移动到这儿单词发声  n.智力;消息   (初中英语单词)
  • actually [´æktʃuəli] 移动到这儿单词发声  ad.事实上;实际上   (初中英语单词)
  • comparison [kəm´pærisən] 移动到这儿单词发声  n.比较;对照;比喻   (初中英语单词)
  • operator [´ɔpəreitə] 移动到这儿单词发声  n.操作者;接线员   (初中英语单词)
  • argument [´ɑ:gjumənt] 移动到这儿单词发声  n.辩论;争论;论证   (初中英语单词)
  • program [´prəugræm] 移动到这儿单词发声  n.说明v.为…安排节目   (初中英语单词)
  • assert [ə´sə:t] 移动到这儿单词发声  vt.认定;维护;坚持   (初中英语单词)
  • comment [´kɔment] 移动到这儿单词发声  n.&v.评论;评注;注意   (初中英语单词)
  • character [´kæriktə] 移动到这儿单词发声  n.特性;性质;人物;字   (初中英语单词)
  • freely [´fri:li] 移动到这儿单词发声  ad.自由地;慷慨地   (初中英语单词)
  • operate [´ɔpəreit] 移动到这儿单词发声  v.(使)运转;操作;经营   (初中英语单词)
  • briefly [´bri:fli] 移动到这儿单词发声  ad.简短地;简略地   (初中英语单词)
  • system [´sistəm] 移动到这儿单词发声  n.系统,体系,制度   (初中英语单词)
  • available [ə´veiləbəl] 移动到这儿单词发声  a.可用的;有效的   (初中英语单词)
  • screen [skri:n] 移动到这儿单词发声  n.银幕 vt.遮蔽   (初中英语单词)
  • partly [´pɑ:tli] 移动到这儿单词发声  ad.部分地;不完全地   (初中英语单词)
  • obvious [´ɔbviəs] 移动到这儿单词发声  a.明显的;显而易见的   (初中英语单词)
  • beginning [bi´giniŋ] 移动到这儿单词发声  n.开始,开端;起源   (初中英语单词)
  • function [´fʌŋkʃən] 移动到这儿单词发声  n.机能;职责 vi.活动   (初中英语单词)
  • review [ri´vju:] 移动到这儿单词发声  v.&n.复习;回顾;检查   (初中英语单词)
  • contain [kən´tein] 移动到这儿单词发声  v.包含;容纳;抑制   (初中英语单词)
  • spelling [´speliŋ] 移动到这儿单词发声  n.拼法;缀字   (初中英语单词)
  • reading [´ri:diŋ] 移动到这儿单词发声  n.(阅)读;朗读;读物   (初中英语单词)
  • finding [´faindiŋ] 移动到这儿单词发声  n.发现物;判断;结果   (高中英语单词)
  • execution [,eksi´kju:ʃən] 移动到这儿单词发声  n.执行;演奏;表演   (高中英语单词)
  • procedure [prə´si:dʒə] 移动到这儿单词发声  n.过程;手续;方法   (高中英语单词)
  • namely [´neimli] 移动到这儿单词发声  ad.即,也就是   (高中英语单词)
  • symbol [´simbəl] 移动到这儿单词发声  n.符号;象征   (高中英语单词)
  • edition [i´diʃən] 移动到这儿单词发声  n.版本;很相似的   (高中英语单词)
  • correspond [,kɔri´spɔnd] 移动到这儿单词发声  vi.符合;相当;通信   (高中英语单词)
  • implement [´implimənt] 移动到这儿单词发声  n.工具 vt.执行   (高中英语单词)
  • ending [´endiŋ] 移动到这儿单词发声  n.结尾,结局   (高中英语单词)
  • significance [sig´nifikəns] 移动到这儿单词发声  n.意义;重要性   (高中英语单词)
  • ignore [ig´nɔ:] 移动到这儿单词发声  vt.忽视,不理,不顾   (高中英语单词)
  • maximum [´mæksiməm] 移动到这儿单词发声  n.极点 a.最大的   (高中英语单词)
  • seeing [si:iŋ] 移动到这儿单词发声  see的现在分词 n.视觉   (高中英语单词)
  • positive [´pɔzətiv] 移动到这儿单词发声  a.确定的   (高中英语单词)
  • faintly [´feintli] 移动到这儿单词发声  ad.微弱地,软弱无力的   (高中英语单词)
  • correctly [kə´rektli] 移动到这儿单词发声  ad.正确地;恰当地   (高中英语单词)
  • appropriate [ə´prəupri-it, ə´prəuprieit] 移动到这儿单词发声  a.适宜的 vt.私占;拨给   (高中英语单词)
  • trying [´traiiŋ] 移动到这儿单词发声  a.难堪的;费劲的   (英语四级单词)
  • sequence [´si:kwəns] 移动到这儿单词发声  n.继续;顺序;程序   (英语四级单词)
  • eventually [i´ventʃuəli] 移动到这儿单词发声  ad.最后,终于   (英语四级单词)
  • definition [,defi´niʃən] 移动到这儿单词发声  n.限定;定义;明确   (英语四级单词)
  • binding [´baindiŋ] 移动到这儿单词发声  a.捆绑的 n.捆绑(物)   (英语四级单词)
  • temporarily [´tempərərili] 移动到这儿单词发声  ad.暂时地   (英语四级单词)
  • utility [ju:´tiliti] 移动到这儿单词发声  n.有用 a.有各种用途的   (英语四级单词)
  • arithmetic [ə´riθmətik] 移动到这儿单词发声  n.算术   (英语四级单词)
  • clause [klɔ:z] 移动到这儿单词发声  n.条(款);分句,从句   (英语四级单词)
  • reasonably [´ri:zənəbli] 移动到这儿单词发声  ad.有理地;合理地   (英语四级单词)
  • presentation [,prezən´teiʃən] 移动到这儿单词发声  n.介绍;赠送;提出   (英语四级单词)
  • variable [´veəriəbəl] 移动到这儿单词发声  a.易变的 n.可变量   (英语六级单词)
  • normally [´nɔ:məli] 移动到这儿单词发声  ad.正常情况下;通常   (英语六级单词)
  • mathematical [,mæθə´mætikəl] 移动到这儿单词发声  a.数学的;精确的   (英语六级单词)
  • reminder [ri´maində] 移动到这儿单词发声  n.提醒物;纪念品;暗示   (英语六级单词)
  • presumably [pri´zju:məbli] 移动到这儿单词发声  ad.推测起来;大概   (英语六级单词)
  • assignment [ə´sainmənt] 移动到这儿单词发声  n.分配;分派;任务   (英语六级单词)
  • revision [ri´viʒən] 移动到这儿单词发声  n.修订(本);修改   (英语六级单词)
  • fitting [´fitiŋ] 移动到这儿单词发声  a.适当的 n.试衣   (英语六级单词)
  • novice [´nɔvis] 移动到这儿单词发声  n.新手;初学者   (英语六级单词)
  • unbound [ʌn´baund] 移动到这儿单词发声  unbind的过去式(分词)   (英语六级单词)


  • 文章总共2页
    文章标签:词典  

    章节正文