List 如何在lisp中对列表排序?
我在lisp中有一个列表,如下所示:List 如何在lisp中对列表排序?,list,sorting,lisp,autocad,autolisp,List,Sorting,Lisp,Autocad,Autolisp,我在lisp中有一个列表,如下所示: ( ((5 6) (2 7)) ((5 4) (2 9)) ((1 8) (7 7)) ) 我想根据所有这些条件对其进行排序: 第一个元素:(56),(54),(18) 在这些元素中,首先按x排序,然后按y排序:(18)(54)(56) 最后,我希望在第一个元素中有一个按上述条件排序的列表,这些元素中的每一个都有第二个元素: ( ((1 8) (7 7)) ((5 4) (2 9)) ((5 6) (2
(
((5 6) (2 7))
((5 4) (2 9))
((1 8) (7 7))
)
我想根据所有这些条件对其进行排序:
(56)
,(54)
,(18)
(18)
(54)
(56)
(
((1 8) (7 7))
((5 4) (2 9))
((5 6) (2 7))
)
你能给我一个子程序吗
谢谢。我忍不住回答了这个问题,因为我试图思考是否有一种排序算法,它很容易用Lisp编写,但却相当糟糕,我得出了这个结论(请参阅注释:该算法一定很有名,但我不知道它的名称) 请注意,这里有很多有意的重新设计:代码不是有意地不透明,而是有意地难以作为家庭作业答案提交 这可能不是你们所说的“口齿不清”的意思
#lang racket
(define (remove/one e l #:test (equivalent? eqv?))
;; remove the first occurence of e from l using equivalent?
;; as the equivalence predicate.
(let loop ([lt l] [a '()])
(cond
[(null? lt)
l]
[(equivalent? e (first lt))
(append (reverse a) (rest lt))]
[else
(loop (rest lt) (cons (first lt) a))])))
(define (extremum l <?)
;; find the extremum of l under <?
(if (null? l)
l
(let loop ([lt (rest l)] [candidate (first l)])
(cond
[(null? lt)
candidate]
[(<? (first lt) candidate)
(loop (rest lt) (first lt))]
[else
(loop (rest lt) candidate)]))))
(define (terrible-sort l less-than?
#:key (key identity))
;; A terrible sort function. This works by repeatedly finding the extremum
;; of l & then recursing on l with the extremum removed.
;; less-than? is assumed to provide a partial order on the elements of l:
;; equivalence is defined by less-than?. key is a key extractor in the
;; usual way: there is no Schwartzian transform though.
;;
;; I haven't stopped to think about the complexity of this but it's at least
;; quadratic (and I think it probably is quadratic?). It's also very consy.
;;
;; This algorithm must have a name.
;;
(define (>? a b)
(less-than? (key b) (key a)))
(define (=? a b)
(let ([av (key a)]
[bv (key b)])
(and (not (less-than? av bv))
(not (less-than? bv av)))))
(let loop ([lt l] [sorted '()])
(if (null? lt)
sorted
(let ([smallest (extremum lt >?)])
(loop (remove/one smallest lt #:test =?)
(cons smallest sorted))))))
(define (answer l)
(terrible-sort l (λ (a b)
;; compare two lists of numbers
(let loop ([at a] [bt b])
(if (null? at)
(if (null? bt)
#f
(error "unequal lengths"))
(match-let ([(cons ath att) at]
[(cons bth btt) bt])
(cond
[(< ath bth) #t]
[(> ath bth) #f]
[else (loop att btt)])))))
#:key first))
(define data '(((5 6) (2 7))
((5 4) (2 9))
((1 8) (7 7))))
#朗球拍
(定义(移除/一个e l#:测试(等效?eqv?))
;;使用等价物从l中删除第一个出现的e?
作为等价谓词。
(让循环([lt l][a'()]))
(续)
[(空?lt)
l]
[(相当于?e(第一个lt))
(附加(反向a)(剩余lt))]
[其他
(循环(剩余左)(cons(第一左)a))))
(define(extremum l我忍不住回答了这个问题,因为我试图思考是否有一种排序算法,它很容易用Lisp编写,但相当糟糕,我得出了这个结论(参见注释:该算法一定很有名,但我不知道它的名称)
请注意,这里有很多有意的重新设计:代码不是有意地不透明,而是有意地难以作为家庭作业答案提交
这可能不是你们所说的“口齿不清”的意思
#lang racket
(define (remove/one e l #:test (equivalent? eqv?))
;; remove the first occurence of e from l using equivalent?
;; as the equivalence predicate.
(let loop ([lt l] [a '()])
(cond
[(null? lt)
l]
[(equivalent? e (first lt))
(append (reverse a) (rest lt))]
[else
(loop (rest lt) (cons (first lt) a))])))
(define (extremum l <?)
;; find the extremum of l under <?
(if (null? l)
l
(let loop ([lt (rest l)] [candidate (first l)])
(cond
[(null? lt)
candidate]
[(<? (first lt) candidate)
(loop (rest lt) (first lt))]
[else
(loop (rest lt) candidate)]))))
(define (terrible-sort l less-than?
#:key (key identity))
;; A terrible sort function. This works by repeatedly finding the extremum
;; of l & then recursing on l with the extremum removed.
;; less-than? is assumed to provide a partial order on the elements of l:
;; equivalence is defined by less-than?. key is a key extractor in the
;; usual way: there is no Schwartzian transform though.
;;
;; I haven't stopped to think about the complexity of this but it's at least
;; quadratic (and I think it probably is quadratic?). It's also very consy.
;;
;; This algorithm must have a name.
;;
(define (>? a b)
(less-than? (key b) (key a)))
(define (=? a b)
(let ([av (key a)]
[bv (key b)])
(and (not (less-than? av bv))
(not (less-than? bv av)))))
(let loop ([lt l] [sorted '()])
(if (null? lt)
sorted
(let ([smallest (extremum lt >?)])
(loop (remove/one smallest lt #:test =?)
(cons smallest sorted))))))
(define (answer l)
(terrible-sort l (λ (a b)
;; compare two lists of numbers
(let loop ([at a] [bt b])
(if (null? at)
(if (null? bt)
#f
(error "unequal lengths"))
(match-let ([(cons ath att) at]
[(cons bth btt) bt])
(cond
[(< ath bth) #t]
[(> ath bth) #f]
[else (loop att btt)])))))
#:key first))
(define data '(((5 6) (2 7))
((5 4) (2 9))
((1 8) (7 7))))
#朗球拍
(定义(移除/一个e l#:测试(等效?eqv?))
;;使用等价物从l中删除第一个出现的e?
作为等价谓词。
(让循环([lt l][a'()]))
(续)
[(空?lt)
l]
[(相当于?e(第一个lt))
(附加(反向a)(剩余lt))]
[其他
(循环(剩余左)(cons(第一左)a))))
(define(extremum l由于您在注释中声明使用的是Visual LISP,因此可以按以下方式使用标准函数(实现快速排序算法):
(setq l
'(
((5 6) (2 7))
((5 4) (2 9))
((1 8) (7 7))
)
)
(vl排序l
"(lambda(a b)
(如果(=(caar a)(caar b))
(<(地籍a)(地籍b))
(<(caar a)(caar b))
)
)
)
这里,lambda比较函数中的if
语句测试每个项的第一个子列表的第一个元素(“x坐标”)是否相等,如果相等,则比较第二个元素(“y坐标”)
对于lambda
函数中的给定项对:
a = ((5 6) (2 7))
(car a) = (5 6)
(caar a) = 5
(cadar a) = 6
由于您在注释中声明正在使用Visual LISP,因此可以按以下方式使用标准函数(实现快速排序算法):
(setq l
'(
((5 6) (2 7))
((5 4) (2 9))
((1 8) (7 7))
)
)
(vl排序l
"(lambda(a b)
(如果(=(caar a)(caar b))
(<(地籍a)(地籍b))
(<(caar a)(caar b))
)
)
)
这里,lambda比较函数中的if
语句测试每个项的第一个子列表的第一个元素(“x坐标”)是否相等,如果相等,则比较第二个元素(“y坐标”)
对于lambda
函数中的给定项对:
a = ((5 6) (2 7))
(car a) = (5 6)
(caar a) = 5
(cadar a) = 6
我找到了一个解决办法:
(defun Sort ()
(setq li nil)
(setq liso nil)
(setq newptlist nil)
(defun AS:Sort (lst / newptlist)
(setq xvals (list))
(foreach pt lst
(if (not (vl-remove-if-not
'(lambda (x) (equal (car (car pt)) x 0.0001))
xvals
)
)
(setq xvals (cons (car (car pt)) xvals))
)
)
(setq xvals (vl-sort xvals '(lambda (x1 x2) (< x1 x2))))
(foreach xval xvals
(setq pts (vl-remove-if-not
'(lambda (x) (equal xval (car (car x)) 0.0001))
lst
)
pts (vl-sort
pts
'(lambda (pt1 pt2) (< (cadr (car pt1)) (cadr (car pt2))))
)
newptlist (append newptlist pts)
)
)
)
(setq li (list (list '(5 6) '(2 7))
(list '(5 4) '(2 9))
(list '(1 8) '(7 7))
)
)
(setq liso (AS:Sort li1))
;;; PRINT
(print "li= ")
(print li)
(print "liso= ")
(print liso)
(princ)
)
(取消排序()
(setq li nil)
(setq liso nil)
(setq NEWPLIST nil)
(定义为:排序(lst/NEWPLIST)
(setq xVAL(列表))
(foreach pt lst)
(如果(不是)(如果不是,则移除vl
'(λ(x)(等于(汽车(汽车pt))x 0.0001))
xvals
)
)
(设定值xVAL(cons(车辆(车辆pt))xVAL))
)
)
(setq xVAL(vl排序xVAL’(λ(x1 x2)(
我找到了解决方法:
(defun Sort ()
(setq li nil)
(setq liso nil)
(setq newptlist nil)
(defun AS:Sort (lst / newptlist)
(setq xvals (list))
(foreach pt lst
(if (not (vl-remove-if-not
'(lambda (x) (equal (car (car pt)) x 0.0001))
xvals
)
)
(setq xvals (cons (car (car pt)) xvals))
)
)
(setq xvals (vl-sort xvals '(lambda (x1 x2) (< x1 x2))))
(foreach xval xvals
(setq pts (vl-remove-if-not
'(lambda (x) (equal xval (car (car x)) 0.0001))
lst
)
pts (vl-sort
pts
'(lambda (pt1 pt2) (< (cadr (car pt1)) (cadr (car pt2))))
)
newptlist (append newptlist pts)
)
)
)
(setq li (list (list '(5 6) '(2 7))
(list '(5 4) '(2 9))
(list '(1 8) '(7 7))
)
)
(setq liso (AS:Sort li1))
;;; PRINT
(print "li= ")
(print li)
(print "liso= ")
(print liso)
(princ)
)
(取消排序()
(setq li nil)
(setq liso nil)
(setq NEWPLIST nil)
(定义为:排序(lst/NEWPLIST)
(setq xVAL(列表))
(foreach pt lst)
(如果(不是)(如果不是,则移除vl
'(λ(x)(等于(汽车(汽车pt))x 0.0001))
xvals
)
)
(设定值xVAL(cons(车辆(车辆pt))xVAL))
)
)
(setq xVAL(vl排序xVAL’(λ(x1 x2)(
你能证明自己曾试图解决这个问题吗?什么