lisp - Lisp 等级到字母的转换
问题描述
问的问题:
Define a LISP function SCORE->GRADE which takes a single argument, s, and returns a symbol according to the following scheme:
s ≥ 90 A 73 ≤ s < 77 C+
87 ≤ s < 90 A– 70 ≤ s < 73 C
83 ≤ s < 87 B+ 60 ≤ s < 70 D
80 ≤ s < 83 B s < 60 F
77 ≤ s < 80 B–
If the argument s is not a number then the function should return NIL.
我的回答是这样的:
(defun SCORE->GRADE (s)
(if (not (numberp s)) (return-from SCORE->GRADE “NIL”))
(progn
(if (>= s 90) (return-from SCORE->GRADE "A"))
(if (and (>= s 87) (< s 90)) (format nil “A-“))
(if (and (>= s 83) (< s 87)) (format nil “B+”))
(if (and (>= s 80) (< s 83)) (return-from SCORE->GRADE “B”))
(if (and (>= s 77) (< s 80)) (return-from SCORE->GRADE “B-“))
(if (and (>= s 73) (< s 77)) (return-from SCORE->GRADE “C+”))
(if (and (>= s 70) (< s 73)) (return-from SCORE->GRADE “C”))
(if (and (>= s 60) (< s 70)) (return-from SCORE->GRADE “D”)
(if (< s 60) (return-from SCORE->GRADE “F”))
)
)
)
它适用于 90,返回 A,然后对于其他任何东西,它只会给出这个错误,关于我输入的内容有不同的变量
*** - RETURN-FROM:变量“B”没有值
*** - IF:变量“A-”没有值
任何人都可以解释为什么我不能为每条令人难以置信的相同的行得到相同的结果吗?
我已经尝试过消息、格式 t、案例,有些最多可用于前 3 个案例,然后停止。一直想不通。
解决方案
除了其他答案之外,请注意,在您的情况下,您不需要复制公共边界,因为您正在尝试将分数划分为等级。
(cond
((>= s 90) "A")
((>= s 87) "A-")
((>= s 83) "B+")
...
((>= s 70) "C")
((>= s 60) "D")
(t "F"))
它还减少了您必须在代码中保留的不变量的数量,这有助于维护。
推荐阅读
- c# - 如何使用聊天机器人在 servicenow 中创建事件
- visual-studio-code - VS Code - 如何在资源管理器中更改灰色文件夹名称的颜色?
- javascript - Javascript“in”给出错误的结果
- ionic-framework - 图像未在 Ionic 3 中加载
- ios - 如何使用 Alamofire 从缓存和网络加载数据?
- c++ - 如何使用文件作为数据库创建字母顺序?C++
- python - 按规则列表排序一些元素的列表
- php - 尝试在 Laravel 中使用 belongsTo 但它不起作用
- performance - JMeter中部分用户的部分注销
- docker - Docker 命令的一部分