Common lisp 在CommonLisp中创建方法

Common lisp 在CommonLisp中创建方法,common-lisp,clisp,Common Lisp,Clisp,您好,我正在做一个条件,我只想调用一个方法,如果条件为真,问题是我找不到如何在C-Lisp中创建方法的语法。我对这种语言不熟悉。下面是代码 /* I want to create a method here which i can all anytime in my condition but I am having problem with a syntax (void method() (print "Invalid") ) */ (print "Enter number"

您好,我正在做一个条件,我只想调用一个方法,如果条件为真,问题是我找不到如何在C-Lisp中创建方法的语法。我对这种语言不熟悉。下面是代码

/* I want to create a method here which i can all anytime in my condition but I am having problem with a syntax 

 (void method()
   (print "Invalid")
 )

*/

(print "Enter number") 
(setq number(read())
(cond((< 1 number) (print "Okay"))
     ((> 1 number) /*I want to call a method here (the invalid one)*/ )
) 
/*我想在这里创建一个方法,在我的条件下,我可以随时使用它,但语法有问题
(无效方法()
(打印“无效”)
)
*/
(打印“输入编号”)
(setq编号(读取())
(条件((<1号)(打印“OK”))
((>1号)/*我想在这里调用一个方法(无效的方法)*/)
) 

要在common lisp中创建函数,可以使用
defun
操作符:

(defun signal-error (msg)
   (error msg))
现在你可以这样称呼它:

(signal-error "This message will be signalled as the error message")
然后,您可以将其插入代码中,如下所示:

(print "Enter number") 
(setq number (read)) ;; <- note that you made a syntax error here.
(cond ((< 1 number) (print "Okay"))
      ((> 1 number) (signal-error "Number is smaller than 1."))))
要为使用的每个类创建特定的方法
defmethod

(defmethod greet ((thing human))
  (print "Hi human!"))

(defmethod greet ((thing dog))
  (print "Wolf-wolf dog!"))
让我们为每个类创建两个实例:

(defparameter Anna (make-instance 'human))
(defparameter Rex (make-instance 'dog))
现在我们可以用同样的方法问候每一个活着的人:

(greet Anna) ;; => "Hi human"
(greet Rex)  ;; => "Wolf-wolf dog!"
公共lisp知道要执行哪个方法的过程称为“动态分派”。基本上,它将给定参数的类与
defmethod
定义相匹配

但我不知道为什么在代码示例中需要方法

如果我是你,我将如何编写代码:

;; Let's wrap the code in a function so we can call it
;; as much as we want
(defun get-number-from-user ()
  (print "Enter number: ")
  ;; wrapping the number in a lexical scope is a good
  ;; programming style. The number variable is not
  ;; needed outside the function.
  (let ((number (read)))
    ;; Here we check if the number satisfies our condition and
    ;; call this function again if not.
    (cond ((< number 1) (print "Number is less than 1")
                        (get-number-from-user))
          ((> number 1) (print "Ok.")))))
;;让我们将代码包装到函数中,以便调用它
我们想要多少就有多少
(取消从用户()获取编号)
(打印“输入编号:”)
将数字包装在词法范围内是一个好方法
;编程风格。数字变量不是
功能之外需要的。
(让((数字(读取)))
在这里,我们检查数字是否满足我们的条件
;如果没有,请再次调用此函数。
(条件((<数字1)(打印“数字小于1”)
(从用户处获取号码))
((>编号1)(打印“确定”。);)

我建议你读一读《Lisp之地》。这是一本适合初学者的好书。

void应该做什么?有关函数的
defun
方法,请参见
defmethod
(没有类型重载)我还建议你研究一下条件系统。这里有一个链接让你开始了解Davids的评论是关于实际的lisp条件的,它类似于其他语言中的例外,但功能更强。我发现你可能已经知道一种编程语言,可能是C族语言之一。很难理解你的第一种语言LISP对ALGOL知识,所以最好是玩绿色,然后去教程或书。当数字是一个时发生什么?@ Sywester同意。OP应该考虑一个类似实用的LISP或类似的教程。是的,但是在代码< COND>代码>测试之后,已经有一个隐含的<代码> PRONN</代码>,所以这一个是多余的。您可能需要一个
finish output
,以确保在请求输入之前显示提示。谢谢!当您尝试帮助其他人学习东西时,学习东西也很好!我更正了
progn
。我现在将熟悉
finish output
。我有时也会收到类似的评论,这肯定会有所帮助。你是说
defclass
而不是
class
?哎哟,更正了。
;; Let's wrap the code in a function so we can call it
;; as much as we want
(defun get-number-from-user ()
  (print "Enter number: ")
  ;; wrapping the number in a lexical scope is a good
  ;; programming style. The number variable is not
  ;; needed outside the function.
  (let ((number (read)))
    ;; Here we check if the number satisfies our condition and
    ;; call this function again if not.
    (cond ((< number 1) (print "Number is less than 1")
                        (get-number-from-user))
          ((> number 1) (print "Ok.")))))