lisp - 如何在 Common lisp 中注意多重条件检查?

标签 lisp common-lisp

才学了几天Common Lisp,教授给我布置了一个习题。但是,我的代码无法编译,谁能告诉我我的编码部分哪里做错了? This is the question

(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)  ; 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 (a b)
  (cond ((not (every #'numberp (list a b))) 'error)
        ((<= a b) a)
        ((> a b) b)))

请缩进您的代码,不要留下括号。

关于lisp - 如何在 Common lisp 中注意多重条件检查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54778426/

相关文章:

struct - Racket 结构错误 : given value instantiates a different structure type with the same name

lisp - 如何反复搜索一个plist?

opengl - 如何使用 SBCL 安装 CL-Opengl?

functional-programming - 计算列表中的原子和列表

java - Clojure into-array 遍历数组

command-line - 在 Common Lisp 中获取命令行参数

lisp - Common Lisp 中的循环列表

common-lisp - Lisp 网络故事 : How to fix the blogdemo example (Chapter 4)?

recursion - 用尾递归解决 "n-rooks"

nested - 通用 Lisp : Appending a nested plist efficiently