通用 Lisp 函数
我想创建一个函数来验证列表中的数字是否在 1 到 10 的范围内(包括 1 和 10)。如果他们这样做,我想将其添加到我创建的新列表中。但是,如果它们未能落入范围内,我决定将它们转换为适合而不是丢弃它们(通过添加 (1+ (随机 10))。
(defun fit (lst)
"(lst) check every number if it fits the range (1-10)"
(do ((fit lst (cdr fit))
(range nil))
((null fit) (reverse range))
(if (and (>= fit 1) (=< fit 10))
(seft ((cons fit range))))
(or (< fit 1) (> fit 10))
(cons (+ fit (1+ (random 10))) range)))
代码从 (defun fit...) 开始。但是,它是不工作,我无法做任何类型的改变,主要是因为我的知识有限。任何帮助或见解将不胜感激。 谢谢。
I want create a function that verify if the numbers of a list fall between the range of 1 and 10 (including the 1 and 10). If they do, I wanted to add it to a new list that I created. However, if they fail to fall within the range I decided to convert them so they fit in rather than discarding them (by adding (1+ (random 10)).
(defun fit (lst)
"(lst) check every number if it fits the range (1-10)"
(do ((fit lst (cdr fit))
(range nil))
((null fit) (reverse range))
(if (and (>= fit 1) (=< fit 10))
(seft ((cons fit range))))
(or (< fit 1) (> fit 10))
(cons (+ fit (1+ (random 10))) range)))
The codes start from (defun fit...). However, it's not working and I'm at short of any type of changes I can do mainly because of my limited knowledge. Any help or insight will be greatly appreciated.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你试图自己做太多的工作。思考更高的功能。您的问题陈述已经告诉您一半的内容:“检查每个数字是否在 1-10 范围内”。
使用(每个)怎么样?然后您需要向其传递一个检查单个数字的函数。可能是拉姆达...
I think you are trying to do too much work yourself. Think higher functions. Your problem statement already tells you half of it: "check every number if it fits in the range 1-10".
How about using (every)? Then you need to pass it a function that checks a single number. Probably lambda...
这个怎么样:
How about this:
我认为您想在每个元素上应用
make-fitting
函数。如果您预期有不同的限制,则应该将它们作为可能的可选参数提供:
I think that you want to apply a
make-fitting
function on each element.If you anticipate different limits, you should give them as possibly optional parameters: