看来我得详细说一下了;这是我的作业。我不想复制你写的代码。我是新手;我正在尝试学习的是如何将主题分解为单个部分,然后专注于我应该使用什么功能来解决问题。我自己完成这些问题有点困难,因为我完全是 Lisp 的新手,实际上是如何编程。我希望你能帮助我。
问题是:有一个给定的常数
(defconstant *storms* '((bob 65)
(chary 150)
(jenny 145)
(ivan 165)
(james 120)))
每个风暴都由其名称和风速列表表示。
风速分类如下:
39–74 → tropical
75–95 → cat-1
96–110 → cat-2
111–130 → cat-3
131–155 → cat-4
156 或更多 →cat-5
现在我必须编写两个函数:
storm-categories
应该生成类别名称,如下所示:(bob tropical)
,(chary cat-1)
, ...并且
storm-distribution
应该生成每个类别中的风暴数,如下所示:(cat-1 1)
,(cat-2 0)
, ...
我尝试解决这个问题的方法是:
使用
if
语句判断风速的类型:(if (and (> x 39) (< x 73)) (print 'tropical)) (if (and (> x 74) (< x 95)) (print 'cat-1)) (if (and (> x 96) (< x 110)) (print 'cat-2)) (if (and (> x 111) (< x 130)) (print'cat-3)) (if (and (> x 131) ( < x 155)) (print'cat-4)) (if (and (> x 156)) (print 'cat-5))
将风速 (like
65
) 替换为 windtype (likecat-1
)(风暴中的 x 循环 做(rplacd x 'windtype)
我只是对第一个功能有一个简单的想法,但仍然不知道如何实现它。我还没有接触过分配功能,因为我仍然坚持第一个。