冒泡排序不返回排序数组

时间:2014-11-10 04:00:27

标签: lisp

这是一个仅由if构成的冒泡排序。这是我在Lisp中的第一个代码,这就是我没有使用函数'loop'或'DO'的原因,因为我不知道如何使用它们。

(defun bubble()
    ((let (array (make-array '(4))))
          (setf (aref array 0) 7)
          (setf (aref array 1) 2)
          (setf (aref array 2) 4)
          (setf (aref array 3) 5))     
    (setf i 0)
    (defun c1 (IF (<= i (- n 1))
         (progn (setq j 1)
               (defun c2 (j) (IF (<= j (- n i))
                                     (progn(IF (> (aref array j) (aref array (+ j 1)))
                                           (progn (setq aux (aref array (+ j 1)))
                                                   (setq (aref array (+ j 1)) (aref array j))
                                                   (setq (aref array j) aux)
                                                   (c2 (setq j (+ j 1))))

                                                 (c2 (setq j (+ j 1)))
                                            );if
                                     );progn

                                     (c1 (setq i (+ i 1))) 
                                 );if
               );c2   
          );progn
         array();
        ) 
     );c1
);bubble

问题是它不打印数组,它只打印单词BUBBLE。有人在前一篇文章中告诉defun总是返回函数的名称,但如果没有defun我怎么能创建一个名为bubble的函数返回一个数组???

2 个答案:

答案 0 :(得分:3)

我认为我们必须把它分开。

如果你使用适当的缩进,你不需要注释你的括号,这使整体编码体验更好:

(defun bubble ()
  ((let (array (make-array '(4))))
   (setf (aref array 0) 7)
   (setf (aref array 1) 2)
   (setf (aref array 2) 4)
   (setf (aref array 3) 5))     
  (setf i 0)
  (defun c1 (IF (<= i (- n 1))
                (progn (setq j 1)
                       (defun c2 (j) (if (<= j (- n i))
                                         (progn (if (> (aref array j) (aref array (+ j 1)))
                                                    (progn (setq aux (aref array (+ j 1)))
                                                           (setq (aref array (+ j 1)) (aref array j))
                                                           (setq (aref array j) aux)
                                                           (c2 (setq j (+ j 1))))
                                                    (c2 (setq j (+ j 1)))))
                                         (c1 (setq i (+ i 1))))))
                array
                ())))

现在,有一些语法错误。我不知道如果没有至少一个警告就可以编译。

您的defun c1 ...表单缺少参数列表。

(defun bubble ()
  ((let (array (make-array '(4))))
   (setf (aref array 0) 7)
   (setf (aref array 1) 2)
   (setf (aref array 2) 4)
   (setf (aref array 3) 5))     
  (setf i 0)
  (defun c1 ()
    (if (<= i (- n 1))
        (progn (setq j 1)
               (defun c2 (j) (if (<= j (- n i))
                                 (progn (if (> (aref array j) (aref array (+ j 1)))
                                            (progn (setq aux (aref array (+ j 1)))
                                                   (setq (aref array (+ j 1)) (aref array j))
                                                   (setq (aref array j) aux)
                                                   (c2 (setq j (+ j 1))))
                                            (c2 (setq j (+ j 1)))))
                                 (c1 (setq i (+ i 1))))))
        array
        ())))

您的let表单完全混乱。 Let可以创建多个绑定,因此您需要将绑定包装在列表中。 let表单也不是有效的运算符。它的身体需要在形式内。绑定只有正文的范围。

(defun bubble ()
  (let ((array (make-array '(4))))
    (setf (aref array 0) 7)
    (setf (aref array 1) 2)
    (setf (aref array 2) 4)
    (setf (aref array 3) 5)
    (setf i 0)
    (defun c1 ()
      (if (<= i (- n 1))
          (progn (setq j 1)
                 (defun c2 (j) (if (<= j (- n i))
                                   (progn (if (> (aref array j) (aref array (+ j 1)))
                                              (progn (setq aux (aref array (+ j 1)))
                                                     (setq (aref array (+ j 1)) (aref array j))
                                                     (setq (aref array j) aux)
                                                     (c2 (setq j (+ j 1))))
                                              (c2 (setq j (+ j 1)))))
                                   (c1 (setq i (+ i 1))))))
          array
          ()))))

if中的c1表单有四个参数,但if只有三个参数。我会在最后删除(),这看起来很荒谬:

(defun bubble ()
  (let ((array (make-array '(4))))
    (setf (aref array 0) 7)
    (setf (aref array 1) 2)
    (setf (aref array 2) 4)
    (setf (aref array 3) 5)
    (setf i 0)
    (defun c1 ()
      (if (<= i (- n 1))
          (progn (setq j 1)
                 (defun c2 (j) (if (<= j (- n i))
                                   (progn (if (> (aref array j) (aref array (+ j 1)))
                                              (progn (setq aux (aref array (+ j 1)))
                                                     (setq (aref array (+ j 1)) (aref array j))
                                                     (setq (aref array j) aux)
                                                     (c2 (setq j (+ j 1))))
                                              (c2 (setq j (+ j 1)))))
                                   (c1 (setq i (+ i 1))))))
          array))))

不要试图嵌套defun。这不像你认为的那样有效。它仍然会在不同的时间定义全局函数定义。对于本地函数定义,请使用labelsflet

(defun bubble ()
  (let ((array (make-array '(4))))
    (setf (aref array 0) 7)
    (setf (aref array 1) 2)
    (setf (aref array 2) 4)
    (setf (aref array 3) 5)
    (setf i 0)
    (labels ((c1 ()
               (if (<= i (- n 1))
                   (progn (setq j 1)
                          (labels ((c2 (j)
                                     (if (<= j (- n i))
                                         (progn (if (> (aref array j) (aref array (+ j 1)))
                                                    (progn (setq aux (aref array (+ j 1)))
                                                           (setq (aref array (+ j 1)) (aref array j))
                                                           (setq (aref array j) aux)
                                                           (c2 (setq j (+ j 1))))
                                                    (c2 (setq j (+ j 1)))))
                                         (c1 (setq i (+ i 1))))))))
                   array))))))

让我们让数组初始化更短一些:

(defun bubble ()
  (let ((array (copy-seq #(7 2 4 5))))
    (setf i 0)
    (labels ((c1 ()
               (if (<= i (- n 1))
                   (progn (setq j 1)
                          (labels ((c2 (j)
                                     (if (<= j (- n i))
                                         (progn (if (> (aref array j) (aref array (+ j 1)))
                                                    (progn (setq aux (aref array (+ j 1)))
                                                           (setq (aref array (+ j 1)) (aref array j))
                                                           (setq (aref array j) aux)
                                                           (c2 (setq j (+ j 1))))
                                                    (c2 (setq j (+ j 1)))))
                                         (c1 (setq i (+ i 1))))))))
                   array))))))

Auxnji未定义。通过设置它们,您可以创建可能特殊的新全局变量。不要那样。首先使用let为它们创建绑定。此外,单个表单不需要progn

(defun bubble ()
  (let ((array (copy-seq #(7 2 4 5)))
        (n 4)
        (i 0)
        (aux 0))
    (labels ((c1 ()
               (if (<= i (- n 1))
                   (let ((j 1))
                     (labels ((c2 (j)
                                (if (<= j (- n i))
                                    (if (> (aref array j) (aref array (+ j 1)))
                                        (progn (setq aux (aref array (+ j 1)))
                                               (setq (aref array (+ j 1)) (aref array j))
                                               (setq (aref array j) aux)
                                               (c2 (setq j (+ j 1))))
                                        (c2 (setq j (+ j 1))))
                                    (c1 (setq i (+ i 1))))))))
                   array))))))

我认为我们现在已经涵盖了句法问题。我们来简化吧。

可以使用rotatef交换两个(或更多)位置,您无需为此创建临时变量。有一个函数1+用于向事物添加1。

(defun bubble ()
  (let ((array (copy-seq #(7 2 4 5)))
        (n 4)
        (i 0))
    (labels ((c1 ()
               (if (<= i (- n 1))
                   (let ((j 1))
                     (labels ((c2 (j)
                                (if (<= j (- n i))
                                    (if (> (aref array j) (aref array (1+ j)))
                                        (progn (rotatef (aref array j)
                                                        (aref array (1+ j)))
                                               (c2 (setq j (1+ j))))
                                        (c2 (setq j (1+ j))))
                                    (c1 (setq i (1+ i))))))))
                   array))))))

增加变量的值并将其存储回该变量是使用incf

完成的
(defun bubble ()
  (let ((array (copy-seq #(7 2 4 5)))
        (n 4)
        (i 0))
    (labels ((c1 ()
               (if (<= i (- n 1))
                   (let ((j 1))
                     (labels ((c2 (j)
                                (if (<= j (- n i))
                                    (if (> (aref array j) (aref array (1+ j)))
                                        (progn (rotatef (aref array j)
                                                        (aref array (1+ j)))
                                               (c2 (incf j)))
                                        (c2 (incf j)))
                                    (c1 (incf i)))))))
                   array))))))

而不是使用<=针对少于而不是整数进行测试,而是针对整数本身使用<进行测试:

(defun bubble ()
  (let ((array (copy-seq #(7 2 4 5)))
        (n 4)
        (i 0))
    (labels ((c1 ()
               (if (< i n)
                   (let ((j 1))
                     (labels ((c2 (j)
                                (if (< j n)
                                    (if (> (aref array j) (aref array (1+ j)))
                                        (progn (rotatef (aref array j)
                                                        (aref array (1+ j)))
                                               (c2 (incf j)))
                                        (c2 (incf j)))
                                    (c1 (incf i)))))))
                   array))))))

不要重复对c2的内部调用,而是在一个级别之前移动你想要做的事情。

(defun bubble ()
  (let ((array (copy-seq #(7 2 4 5)))
        (n 4)
        (i 0))
    (labels ((c1 ()
               (if (< i n)
                   (let ((j 1))
                     (labels ((c2 (j)
                                (if (< j n)
                                    (progn
                                      (when (> (aref array j)
                                               (aref array (1+ j)))
                                        (rotatef (aref array j)
                                                 (aref array (1+ j))))
                                      (c2 (incf j)))
                                    (c1 (incf i)))))))
                   array))))))

永远不会调用你的内在函数。让我们解决这个问题,并清理参数处理:

(defun bubble ()
  (let ((array (copy-seq #(7 2 4 5)))
        (n 4))
    (labels ((c1 (i)
               (if (< i n)
                   (labels ((c2 (j)
                              (if (< j n)
                                  (progn
                                    (when (> (aref array j)
                                             (aref array (1+ j)))
                                      (rotatef (aref array j)
                                               (aref array (1+ j))))
                                    (c2 (1+ j)))
                                  (c1 (1+ i)))))
                     (c2 1))
                   array)))
      (c1 0))))

J不应该从1开始,而应从i开始。 Ij应该只运行到(1- n)以下,因为(1+ j)必须是有效的数组索引。

(defun bubble ()
  (let ((array (copy-seq #(7 2 4 5)))
        (n 4))
    (labels ((c1 (i)
               (if (< i (1- n))
                   (labels ((c2 (j)
                              (if (< j (1- n))
                                  (progn
                                    (when (> (aref array j)
                                             (aref array (1+ j)))
                                      (rotatef (aref array j)
                                               (aref array (1+ j))))
                                    (c2 (1+ j)))
                                  (c1 (1+ i)))))
                     (c2 i))
                   array)))
      (c1 0))))

我认为只要数组长度小于堆栈限制,现在就可以了。

有意义的是不定义要在函数内部排序的数组,而是将其作为参数传递。这样,您实际上可以使用该函数对任何数组进行排序(实际上是任何向量,即一维数组)。 N是该向量的长度。

(defun bubble (vector)
  (let ((n (length vector)))
    (labels ((c1 (i)
               (if (< i (1- n))
                   (labels ((c2 (j)
                              (if (< j (1- n))
                                  (progn
                                    (when (> (aref vector j)
                                             (aref vector (1+ j)))
                                      (rotatef (aref vector j)
                                               (aref vector (1+ j))))
                                    (c2 (1+ j)))
                                  (c1 (1+ i)))))
                     (c2 i))
                   vector)))
      (c1 0))))

;;; For example, call with (bubble (copy-seq #(7 2 4 5))). It should return #(2 4 5 7).

为了不修改作为参数传递的向量,请在排序之前使用copy-seq制作副本。使用函数修改其参数总是令人惊讶。我们也不会因修改文字数据而遇到潜在的麻烦。

(defun bubble (unsorted-vector)
  (let ((vector (copy-seq unsorted-vector))
        (n (length unsorted-vector)))
    (labels ((c1 (i)
               (if (< i (1- n))
                   (labels ((c2 (j)
                              (if (< j (1- n))
                                  (progn
                                    (when (> (aref vector j)
                                             (aref vector (1+ j)))
                                      (rotatef (aref vector j)
                                               (aref vector (1+ j))))
                                    (c2 (1+ j)))
                                  (c1 (1+ i)))))
                     (c2 i))
                   vector)))
      (c1 0))))

;;; For example, call with (bubble #(7 2 4 5)). It should return #(2 4 5 7).

为了消除堆栈限制,让我们将其转换为循环。

(defun bubble (unsorted-vector)
  (let ((vector (copy-seq unsorted-vector))
        (n (length unsorted-vector)))
    (loop :for i :below (1- n)
      :do (loop :for j :from i :below (1- n)
            :do (when (> (aref vector j)
                         (aref vector (1+ j)))
                  (rotatef (aref vector j)
                           (aref vector (1+ j))))))
    vector))

答案 1 :(得分:2)

您必须在定义后调用该函数。

相关问题