lisp - 如何在 Common lisp 中进行多重条件检查?
问题描述
刚学了几天Common Lisp,但是教授给我布置了一个练习。但是,我的代码无法编译,谁能告诉我我的编码部分哪里出错了?
(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)))
并且请缩进你的代码,不要把括号放在周围。
推荐阅读
- python - 使用 python 后端制作多人回合制在线游戏的最佳方法
- angular - Angular 在 url 删除 & 或 %26 之后的所有内容
- python - Pandas:如何将 Series 的 MultiIndex 折叠为 DateTimeIndex?
- bash - if else 语句 bash 脚本
- python-3.x - 按区域对所有连接的组件进行排序并导出它们失败,如何解决这个问题?
- arduino - 多普勒雷达模拟读取()
- r - 过滤以字符串开头的条目 (str_detect)
- c# - 从 C# 调用 C++ DLib 导致错误分配异常
- vba - 激活/停用超链接
- google-apps-script - Google 表格中的非典型合并(只有少数常见列)