通用 Lisp 函数

发布于 2024-12-13 18:44:29 字数 563 浏览 0 评论 0原文

我想创建一个函数来验证列表中的数字是否在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愿与i 2024-12-20 18:44:29

我认为你试图自己做太多的工作。思考更高的功能。您的问题陈述已经告诉您一半的内容:“检查每个数字是否在 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...

忱杏 2024-12-20 18:44:29

这个怎么样:

(defun fit (list)
  (cond ((null list) '())
        ((< (car list) 1)
         (fit (cons (+ (car list) (random 10)) (cdr list))))
        ((> (car list) 10)
         (fit (cons (- (car list) (random 10)) (cdr list))))
        (t (cons (car list) (fit (cdr list))))))

How about this:

(defun fit (list)
  (cond ((null list) '())
        ((< (car list) 1)
         (fit (cons (+ (car list) (random 10)) (cdr list))))
        ((> (car list) 10)
         (fit (cons (- (car list) (random 10)) (cdr list))))
        (t (cons (car list) (fit (cdr list))))))
谎言 2024-12-20 18:44:29

我认为您想在每个元素上应用 make-fitting 函数。

(defun make-fitting (n)
  (loop (incf n
              (cond ((> n 10) (- (1+ (random 10))))
                    ((< n 1) (1+ (random 10)))
                    (t (return-from make-fitting n))))))

(defun fit (list)
  (mapcar #'make-fitting list))

如果您预期有不同的限制,则应该将它们作为可能的可选参数提供:

(defun make-fitting (n &optional (low 1) (high 10))
  (let ((step-size (1+ (- high low))))
    (flet ((random-step (size)
             (1+ (random size))))
      (loop (incf n
                  (cond ((> n high) (- (random-step step-size)))
                        ((< n low) (random-step step-size))
                        (t (return-from make-fitting n))))))))

I think that you want to apply a make-fitting function on each element.

(defun make-fitting (n)
  (loop (incf n
              (cond ((> n 10) (- (1+ (random 10))))
                    ((< n 1) (1+ (random 10)))
                    (t (return-from make-fitting n))))))

(defun fit (list)
  (mapcar #'make-fitting list))

If you anticipate different limits, you should give them as possibly optional parameters:

(defun make-fitting (n &optional (low 1) (high 10))
  (let ((step-size (1+ (- high low))))
    (flet ((random-step (size)
             (1+ (random size))))
      (loop (incf n
                  (cond ((> n high) (- (random-step step-size)))
                        ((< n low) (random-step step-size))
                        (t (return-from make-fitting n))))))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文