【问题标题】:CLisp - sorting and combining two lists in quicksortCLisp - 在快速排序中对两个列表进行排序和组合
【发布时间】:2019-09-25 19:55:22
【问题描述】:

我正在尝试在 CLisp 中实现快速排序,到目前为止,我能够围绕一个枢轴对列表进行分区。但是,当我尝试合并子列表并递归排序时,我得到堆栈溢出或let 错误,我不确定出了什么问题。这是我的代码:

(defun pivot (n xs)
  (list (getLesser n xs) (getGreater n xs))
)

(defun getLesser (m l)
  (cond
    ((null l) nil)
    ((<= m (car l)) (getLesser m (cdr l)))
    (t (cons (car l) (getLesser m (cdr l)))))
)

(defun getGreater (m l)
  (cond
    ((null l) nil)
    ((> m (car l)) (getGreater m (cdr l)))
    (t (cons (car l) (getGreater m (cdr l)))))
)


(defun quicksort (xs)
  (cond
    ((null xs) nil)
    (t
      (let (partition (pivot (car xs) xs))
        (cond
          ((null (car partition)) (cons (quicksort (cdr partition)) nil))
          ((null (cdr partition)) (cons (quicksort (car partition)) nil))
          (t (append (quicksort (car partition)) (quicksort (cdr partition)))))))))

我的想法是有一个局部变量partition,它是2 个列表的列表,其中car partition 是小于枢轴的数字列表,cdr partition 是大于枢轴的数字列表。然后,在最后的cond 构造中,如果没有小于枢轴的数字,我将对第二个列表进行递归排序;如果没有大于枢轴的数字,我将对第一个列表进行排序;否则我会递归地对两者进行排序并按顺序附加它们。谁能帮帮我?

【问题讨论】:

    标签: recursion syntax common-lisp let clisp


    【解决方案1】:

    LET 的语法是 (LET BINDINGS . BODY),其中 BINDINGS 是绑定列表;每个绑定都是一个 (SYMBOL VALUE) 列表。或者,绑定可以只是 SYMBOL,它代表 (SYMBOL NIL)。你的代码是:

    (let (partition (pivot (car xs) xs))
      ...)
    

    让我们每行编写一个绑定,并将所有绑定标准化为适当的列表:

    (let ((partition nil)
          (pivot (car xs) xs)))
      ...)
    

    可以看到代码:

    • partition 绑定到 NIL
    • 第二个绑定格式错误:有三个元素,即pivot(car xs)xs,与预期的(SYMBOL VALUE) 语法不匹配。

    【讨论】:

      【解决方案2】:

      编译文件会提示您语法错误。

      GNU CLISP 产生这些诊断:

      $ clisp -q -c foo.lisp
      ;; Compiling file /tmp/foo.lisp ...
      WARNING: in QUICKSORT in lines 20..28 : Illegal syntax in LET/LET*: (PIVOT (CAR XS) XS)
               Ignore the error and proceed
      ;; Deleted file /tmp/foo.fas
      There were errors in the following functions:
       QUICKSORT
      1 error, 1 warning
      

      SBCL 产生类似的诊断结果:

      $ sbcl --eval '(compile-file "foo.lisp")' --quit
      This is SBCL 1.3.1.debian, an implementation of ANSI Common Lisp.
      More information about SBCL is available at <http://www.sbcl.org/>.
      
      SBCL is free software, provided as is, with absolutely no warranty.
      It is mostly in the public domain; some portions are provided under
      BSD-style licenses.  See the CREDITS and COPYING files in the
      distribution for more information.
      ; compiling file "/tmp/foo.lisp" (written 08 MAY 2019 08:58:54 PM):
      ; compiling (DEFUN PIVOT ...)
      ; compiling (DEFUN GETLESSER ...)
      ; compiling (DEFUN GETGREATER ...)
      ; compiling (DEFUN QUICKSORT ...)
      ; file: /tmp/foo.lisp
      ; in: DEFUN QUICKSORT
      ;     (LET (PARTITION (PIVOT (CAR XS) XS))
      ;       (COND ((NULL (CAR PARTITION)) (CONS (QUICKSORT #) NIL))
      ;             ((NULL (CDR PARTITION)) (CONS (QUICKSORT #) NIL))
      ;             (T (APPEND (QUICKSORT #) (QUICKSORT #)))))
      ; 
      ; caught ERROR:
      ;   The LET binding spec (PIVOT (CAR XS) XS) is malformed.
      ; 
      ; compilation unit finished
      ;   caught 1 ERROR condition
      
      
      ; /tmp/foo.fasl written
      ; compilation finished in 0:00:00.021
      

      然后您可以在CLHS 中查找预期的语法:http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/speope_letcm_letst.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-10
        • 1970-01-01
        • 1970-01-01
        • 2016-05-09
        • 1970-01-01
        • 1970-01-01
        • 2021-07-08
        相关资源
        最近更新 更多