1
我需要写的函数(快速排序PRED LST) LST是号码列表进行排序 PRED是通过列表进行排序的谓词,签名这个断言的是:(拉姆达(XY)...)方案快速排序与过滤
- (quick-sort < lst) will sort ascending (small to large)
- (quick-sort > lst) will sort descending (large to small)
- (quick-sort (lambda (x y) (< (car x) (car y))) lst) will sort a list
with inner lists according to the first element of the inner list, ascending.
我开始与常规快速排序:
(define (quick-sort lst)
(cond
((null? lst) '())
((= (length lst) 1) lst)
(else (append (quick-sort (filter (lambda (n) (< n (car lst))) lst))
(list (car lst))
(quick-sort (filter (lambda (n) (> n (car lst))) lst))))))
现在我想用预解码做到这一点:
(define (quick-sort pred lst)
(define (quick-sort-help lst)
(cond ((null? lst)())
((= (length lst) 1) lst)
(else
(append (quick-sort-help (filter (lambda (n) (pred n (car lst))) lst))
(list (car lst))
(quick-sort-help (filter (lambda (n) (not(pred n (car lst)))) lst)))))) (quick-sort-help lst))
我得到一个无限递归或东西。
请问您能帮我解决这个问题吗? 谢谢!
非常感谢!但是,当我试图用它:(qsort(lambda(xy)(<(car x)(car y)))lst)它不起作用......任何想法为什么? (它假设根据内部列表的第一个元素按照内部列表排序 ,升序 – user5500724
如果内部列表为空,但它不起作用,但是'(qsort(lambda(xy)(<(car x )(())((7 2)(3 6)(5 3)))'产生'(list(list 3 6)(list 5 3)(list 7 2))'是不是你想要什么? – AleArk
你说得对,这是我的错,很抱歉,再次感谢! – user5500724