Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Recursion CLisp-在快速排序中对两个列表进行排序和组合_Recursion_Syntax_Common Lisp_Let_Clisp - Fatal编程技术网

Recursion CLisp-在快速排序中对两个列表进行排序和组合

Recursion CLisp-在快速排序中对两个列表进行排序和组合,recursion,syntax,common-lisp,let,clisp,Recursion,Syntax,Common Lisp,Let,Clisp,我正在尝试在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

我正在尝试在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)))))))))
(解除枢轴(n xs)
(列表(较小的n xs)(较大的n xs))
)
(公尺)
(续)
((空l)无)
((m(左车))(G(左车)))
(t(cons(左车)(getGreater m(cdr l()())))
)
(取消快速排序(xs)
(续)
((null xs)nil)
(t
(让(隔板(枢轴(车厢xs)xs))
(续)
((null(汽车分区))(cons(快速排序(cdr分区))nil))
((null(cdr分区))(cons(快速排序(汽车分区))nil))
(t(追加(快速排序(汽车分区))(快速排序(cdr分区()()()())()())(

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

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

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 --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
SBCL产生类似的诊断:

$ 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 --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
$sbcl--eval'(编译文件“foo.lisp”)--退出
这是SBCL 1.3.1.debian,是ANSI Common Lisp的一个实现。
有关SBCL的更多信息,请访问。
SBCL是免费软件,按原样提供,绝对没有保修。
它主要是在公共领域;某些部分在以下章节中提供:
BSD风格的许可证。请参阅中的学分和复制文件
分发以获取更多信息。
; 编译文件“/tmp/foo.lisp”(写于2019年5月8日下午8:58:54):
; 编译(DEFUN PIVOT…)
; 编译(DEFUN GETLESSER…)
; 编译(DEFUN getgreer…)
; 编译(取消快速排序…)
; 文件:/tmp/foo.lisp
; in:DEFUN快速排序
;     (让(隔板(枢轴(车厢XS)XS))
(COND((NULL(汽车分区))(CONS(快速排序)NIL))
;((空(CDR分区))(CONS(快速排序#)NIL))
(T(追加(快速排序);(快速排序);)
; 
; 捕获错误:
;   LET绑定规范(PIVOT(CAR XS)XS)的格式不正确。
; 
; 编译单元已完成
;   捕获1个错误条件
; /tmp/foo.fasl已编写
; 编译在0:00:00.021中完成
然后,您可以在中查找预期语法:

is(LET BINDINGS.BODY)的语法,其中BINDINGS是绑定列表;每个绑定都是一个(符号值)列表。或者,绑定可以是SYMBOL,它表示(SYMBOL NIL)。您的代码是:

(let (partition (pivot (car xs) xs))
  ...)
让我们每行编写一个绑定,并将所有绑定规范化为适当的列表:

(let ((partition nil)
      (pivot (car xs) xs)))
  ...)
您可以看到以下代码:

  • 分区
    绑定到NIL
  • 具有格式不正确的第二个绑定:有三个元素,即
    pivot
    (car-xs)
    xs
    ,它们与预期的(符号值)语法不匹配