在 Common Lisp 中创建方法
Creating a method in Common Lisp
你好我正在做一个条件,我只想在条件为真时调用一个方法,问题是我找不到语法如何在 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)*/ )
)
要在 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."))))
在你的问题中,你问的是 method
。方法在 classes 上运行。例如假设你有两个 classes human
和 dog
:
(defclass human () ())
(defclass dog () ())
要为每个 class 创建一个特定的方法,您使用 defmethod
:
(defmethod greet ((thing human))
(print "Hi human!"))
(defmethod greet ((thing dog))
(print "Wolf-wolf dog!"))
让我们为每个创建两个实例 class:
(defparameter Anna (make-instance 'human))
(defparameter Rex (make-instance 'dog))
现在我们可以用同样的方式问候每一个众生了:
(greet Anna) ;; => "Hi human"
(greet Rex) ;; => "Wolf-wolf dog!"
common lisp知道执行哪个方法的过程叫做"Dynamic dispatch"。基本上它匹配给定参数的 classes 到 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.")))))
我建议您阅读 "The Land of Lisp"。这本书适合初学者。
你好我正在做一个条件,我只想在条件为真时调用一个方法,问题是我找不到语法如何在 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)*/ )
)
要在 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."))))
在你的问题中,你问的是 method
。方法在 classes 上运行。例如假设你有两个 classes human
和 dog
:
(defclass human () ())
(defclass dog () ())
要为每个 class 创建一个特定的方法,您使用 defmethod
:
(defmethod greet ((thing human))
(print "Hi human!"))
(defmethod greet ((thing dog))
(print "Wolf-wolf dog!"))
让我们为每个创建两个实例 class:
(defparameter Anna (make-instance 'human))
(defparameter Rex (make-instance 'dog))
现在我们可以用同样的方式问候每一个众生了:
(greet Anna) ;; => "Hi human"
(greet Rex) ;; => "Wolf-wolf dog!"
common lisp知道执行哪个方法的过程叫做"Dynamic dispatch"。基本上它匹配给定参数的 classes 到 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.")))))
我建议您阅读 "The Land of Lisp"。这本书适合初学者。