如何使用示例函数?

时间:2014-09-22 00:11:04

标签: common-lisp

该程序取自{Norgig,1992,Morgan Kaufmann Publishers,Inc。的Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp。如果我编译并加载到调试窗口,我将如何使用它?

; This function returns a random element of the list choices
(defun random-elt (choices)
    "Choose an element from a list at random."
;; elt returns the (n + 1)th element of the list choices 
;; random returns a random integer no large than the number of
;;   elements in the list choices
    (elt choices (random (length choices))))

; This function returns a random element of the given set and returns 
; it in a list
(defun one-of (set)
     "Pick one element of set, and make a list of it."
     (list (random-elt set)))

; Define a sentence as a noun-phrase + verb phrase
(defun sentence ()  (append (noun-phrase) (verb-phrase)))

; Define a noun phrase as an article + noun
(defun noun-phrase () (append (Article) (Noun)))

; Define a verb phrase as a verb + a noun phrase
(defun verb-phrase () (append (Verb) (noun-phrase)))

; This function returns a randomly selected article
(defun Article () (one-of '(the a)))

; This function returns a randomly selected noun
(defun Noun () (one-of '(man ball woman table)))

; This function returns a randomly selected verb
(defun Verb () (one-of '(hit took saw liked)))

2 个答案:

答案 0 :(得分:3)

查看该代码,我看到另一个未使用的函数是sentence。如果您输入(sentence),您将获得一个随机的句子,如:

(sentence) ;==> (THE WOMAN TOOK A TABLE)

答案 1 :(得分:1)

通常情况下,我会使用SLIME / Emacs,Clozure CL,LispWorks,Allegro CL等编辑文件或其他任何可以与Common Lisp交谈的编辑器。

如果将代码放在缓冲区中,则编译缓冲区。在SLIME / Emacs中,使用 control-c control-k meta-x slime-compile-and-load-file 。这将编译整个文件并将已编译的代码加载到正在运行的下级 Common Lisp中。

在LispWorks IDE中,我只需进入 buffer 菜单并执行 compile

然后转到侦听器(又名 REPL )并执行(sentence)。 Norvig出色的书中的第34至38页详细解释了代码以及如何使用它。