如何注意常见lisp中的多重条件检查?
我刚学了几天CommonLisp,但教授给我安排了一个练习。然而,我的代码无法编译,有人能告诉我我的代码部分哪里出错了吗如何注意常见lisp中的多重条件检查?,lisp,common-lisp,Lisp,Common Lisp,我刚学了几天CommonLisp,但教授给我安排了一个练习。然而,我的代码无法编译,有人能告诉我我的代码部分哪里出错了吗 (除雾(最低2 a b) (续) ((和(numberp a)(numberp b)(定义函数时使用了错误的语法。请使用defun function name(args) (除最低2(a-b) (续) ((和(numberp a)(numberp b)(定义函数时使用了错误的语法。请使用defun function name(args) (除最低2(a-b) (续) ((和)
(除雾(最低2 a b)
(续)
((和(numberp a)(numberp b)(定义函数时使用了错误的语法。请使用defun function name(args)
(除最低2(a-b)
(续)
((和(numberp a)(numberp b)(定义函数时使用了错误的语法。请使用defun function name(args)
(除最低2(a-b)
(续)
((和)(数字a)(数字b)(直译:
(defun min-2 (a b) ; Define a Lisp function MIN-2 … takes two arguments A and B
(cond ((and (every #'numberp (list a b)) (<= a b)) a) ; if … A <= B, returns A
((and (every #'numberp (list a b)) (> a b)) b) ; if … A > B, returns B
(t 'error) ; if A or B is not a number (i. e. “else”), returns ERROR
(defun min-2(ab);定义Lisp函数min-2…采用两个参数a和b
(第二个((和(每一个#'数字)(列表a b))(直译:
(defun min-2 (a b) ; Define a Lisp function MIN-2 … takes two arguments A and B
(cond ((and (every #'numberp (list a b)) (<= a b)) a) ; if … A <= B, returns A
((and (every #'numberp (list a b)) (> a b)) b) ; if … A > B, returns B
(t 'error) ; if A or B is not a number (i. e. “else”), returns ERROR
(defun min-2(ab);定义Lisp函数min-2…采用两个参数a和b
(第二项((和(每一个#'编号)(列表a和b))(我认为编写这样的函数是值得的,因为这样可以明确什么是健全性检查,什么是实际计算。因此,在这种情况下,健全性检查是:'两个参数都是数字吗?'如果它们都是,则计算是对它们的比较。因此,将这两件事分开,而不是将其捆绑到一个条件中:
(defun min-2 (a b)
(if (and (numberp a) (numberp b))
;; sanity check OK, so compare them
(if (<= a b)
a
b)
'error))
我认为编写这样的函数是值得的,因为这样可以明确什么是健全性检查,什么是实际计算。因此,在这种情况下,健全性检查是:“两个参数都是数字吗?”如果它们都是,则计算是对它们的比较。因此,将这两件事分开,而不是将它们捆绑到一个条件中:
(defun min-2 (a b)
(if (and (numberp a) (numberp b))
;; sanity check OK, so compare them
(if (<= a b)
a
b)
'error))
为什么你的代码失败
有人能告诉我我的编码部件哪里出了问题吗
错误(我正在Emacs+SBCL下编译代码):
未定义变量:错误
实际上,error
在这里是一个自由变量。您需要引用它
(defun MIN-2 (a b)
(cond
((and (numberp a) (numberp b) (<= a b))
a
b)
((and (numberp a) (numberp b))
'ERROR)))
子句主体是a
,后跟b
,这意味着a
被计算,其值被丢弃(从未使用),然后b
被计算,其值是cond
表达式的值:当两个输入都是数字时,总是返回b
,这样a就是代码失败的原因
有人能告诉我我的编码部件哪里出了问题吗
错误(我正在Emacs+SBCL下编译代码):
未定义变量:错误
实际上,error
在这里是一个自由变量。您需要引用它
(defun MIN-2 (a b)
(cond
((and (numberp a) (numberp b) (<= a b))
a
b)
((and (numberp a) (numberp b))
'ERROR)))
子句主体是a
,后面跟着b
,这意味着a
被评估,其值被丢弃(从未使用),然后计算b
,其值是cond
表达式的值:当两个输入都是数字时,总是返回b
,这样在公共Lisp中a可以在cond
子句中有任意多个表单。在公共Lisp中,可以在cond
子句中有任意多个表单。您的Lisp编译器在消除(列表a b)
方面有多好?@Kaz:(和(numberp a)(numberp b))
在反汇编中看起来更小。如果您需要性能,请使用它。您能向我解释什么是“在numberp之前,为什么我们需要”?谢谢。@hello:#numberp
的意思是(函数numberp)
表示“名为numberp的函数对象”。我们希望将该函数作为参数传递给高阶函数every
。如果我们只写numberp
,则表示“绑定到变量numberp的值”,这是一个不同的、不相关的东西。您的Lisp编译器在消除(列表a b)
方面有多好?@Kaz:(and(numberp a)(numberp b))
在反汇编中看起来更小。如果您需要性能,请使用它。您能向我解释什么是“在numberp之前,为什么我们需要”吗?谢谢。@hello:#“numberp
表示(function numberp)
表示“名为numberp的函数对象”。我们希望将该函数作为参数传递给高阶函数every
。如果我们只编写numberp
,则表示“绑定到变量numberp的值”,这是一个不同的、不相关的东西。
(defun min-2 (a b)
(if (and (realp a) (realp b))
;; sanity check OK, so compare them
(if (<= a b)
a
b)
'error))
(defun( MIN-2 a b)
(cond
((and (numberp a) (numberp b) (<= a b)) a b)
((and (numberp a) (numberp b) nil) ERROR)
)
)
(defun (MIN-2 a b) ;; << bad syntax, as already pointed out in another answer
(cond
((and (numberp a) (numberp b) (<= a b))
a
b)
((and (numberp a) (numberp b) nil)
ERROR)))
(defun MIN-2 (a b)
(cond
((and (numberp a) (numberp b) (<= a b))
a
b)
((and (numberp a) (numberp b) nil)
ERROR)))
(defun MIN-2 (a b)
(cond
((and (numberp a) (numberp b) (<= a b))
a
b)
((and (numberp a) (numberp b))
'ERROR)))
(and (numberp a) (numberp b) (<= a b))
(defun min-2 (a b)
(handler-case (if (<= a b) a b)
(error () 'error)))
(defun min-2 (a b)
(or (ignore-errors (if (<= a b) a b))
'error))
(defgeneric min-2 (a b)
(:method ((a number) (b number)) (if (<= a b) a b))
(:method (a b) 'error))