common-lisp - Common Lisp 中重复的 case 语句
问题描述
这里必须有更好的方法来做到这一点,对吧?
(format t "Enter your age: ~%")
(defun age-case (age)
(case age
(1 (format t "You belong in Kindergarden~%"))
(2 (format t "You belong in Kindergarden~%"))
(3 (format t "You belong in Kindergarden~%"))
(4 (format t "You belong in Kindergarden~%"))
(5 (format t "You belong in Preschool~%"))
(6 (format t "Elementary school ~%"))
(t (format t "Somewhere else"))))
(defvar *age* (read))
(age-case *age*)
在 Python 中,我会为此使用案例 1..4,在 C++、Java 和 co 中。我可能会使用一个falltrough switch case,其中我省略了案例1到3的中断。在没有代码重复的情况下,是否有一个巧妙的小技巧可以在clisp中做到这一点?
解决方案
另一种选择是使用类型说明符:
CL-USER > (let ((age 6))
(typecase age
((integer 1 4) 'one-to-four) ; integers from 1 to 4
((eql 5) 'five)
((eql 6) 'six)
(t 'something-else)))
SIX
推荐阅读
- python - Discord.py 模拟命令
- php - Magento2.3.4 调整大小的图像不显示在类别页面、购物车页面、结帐页面和迷你购物车中
- java - 级联 if-else 语句和嵌套 if 语句之间是否存在性能差异
- javascript - 如何使用 document.write 更改文本并删除过去的文本
- android-studio - 更新到 Android Studio 4.1.1
- rust - 如何编写一个宏来返回基于字符串的结构的实现方法?
- r - 另一个向量中特定对象的向量中的第二大值
- mysql - 如何根据第 1 列的值仅获取第 2 列的唯一值?
- php - 使用 laravel api 在 swoole 上启用 https
- python - new_row = dict.fromkeys(df, 0) 语句的问题