当枢轴和列表作为输入给出时,如何使分区返回两个列表输出?
我正在尝试使用一个分区在方案中进行QuickSort实现,该分区需要两个参数,一个枢轴和一个列表。
如果我要运行:
=> (partition '3 '(5 7 8 6 4 2 1))
我希望输出是:
=>;Value: ((2 1) (3 5 7 8 6 4))
我正在使用此代码:
(define (partition lt? lst)
(if (null? lst)
(values '() '())
(let ((rest (cdr lst)))
(if (lt? (car lst) (car rest))
(let ((left (partition lt? rest)))
(values (cons (car lst) (car left))
(cdr left)))
(let ((right (partition lt? rest)))
(values (car right)
(cons (car lst) (cdr right)))))))
我会收到以下错误:
I'm trying to do a quicksort implementation in scheme using a partition that takes two argument, a pivot and a list.
If I were to run:
=> (partition '3 '(5 7 8 6 4 2 1))
I want the output to be:
=>;Value: ((2 1) (3 5 7 8 6 4))
I'm using this code:
(define (partition lt? lst)
(if (null? lst)
(values '() '())
(let ((rest (cdr lst)))
(if (lt? (car lst) (car rest))
(let ((left (partition lt? rest)))
(values (cons (car lst) (car left))
(cdr left)))
(let ((right (partition lt? rest)))
(values (car right)
(cons (car lst) (cdr right)))))))
I get the following error:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除了将数字作为一个预计将是一个函数的参数的问题外,您还遇到了另一个大问题。您的
分区
函数返回两个值,但是它是递归的,每次调用它时,您都不会捕获两个值,从而使实际构建分区列表很难。这是几种做到这一点的方法。我建议的第一个是尾部递归的,这要归功于 nate ,并且仅实际使用
value
作为其最终返回值。另一个演示了标准call-with-with-with-with-code>函数,以捕获递归调用返回的两个值。
还有一个测试安全带,演示了捕获多个值的另一种方法, srfi-111
let-values
,它比call-with-with-values
更友好。还有 srfi-8的在这里未显示的另一种方式。
Besides the problem with passing a number as an argument that's expected to be a function, you have another big issue too. Your
partition
function returns two values, but it's recursive and every time you call it, you're not capturing both of them, making it hard to actually build up the partitioned lists.Here's a couple of ways to do it. The first, which I would suggest, is tail recursive thanks to a named let and only actually uses
values
for its final return value. The other one demonstrates the standardcall-with-values
function to capture both values returned by the recursive calls.There's also a test harness that demonstrates another way to capture multiple values, the SRFI-11
let-values
, which tends to be way more friendly thancall-with-values
. There's also SRFI-8'sreceive
as yet another way not shown here.我为你写了
这是一个测试
I wrote this for you
here is a test
您对分区的定义采用一个过程
lt?
和一个列表,但是您将3
作为过程发送。您应该有3个参数,lt?
,pivot
和lst
?这是您的错误消息的来源。 这是对这种错误及其可能来源的更详细的解释。代码中有更多错误。
分区
返回两个值,但是您的代码不会使用适当的步骤来接收更多值,例如。let-values
。一些代码似乎期望有结果列表,而不是两个结果。您还应该开始使用最简单的情况进行测试。我会首先做这些:
Your definition of partition takes a procedure
lt?
and a list, but you are sending3
as the procedure. You should perhaps have 3 arguments,lt?
,pivot
andlst
? This is the source of your error message. Here is a more elaborate explanation of that type of error and its possible sources.There are more errors in the code.
partition
returns two values, but your code does not use the appropriate steps to receive more values when calling itself, eg.let-values
. Some of the code seems to expect a list with results instead of two results.You should also start testing with the simplest cases. I would have done these first: