lisp - 如何在普通的 lisp 中实现自然排序?

标签 lisp common-lisp

我尝试实现自然排序:

Break 21 [92]> (defparameter *sss* '("1.txt" "10.txt" "13.txt" "12.txt" "2.txt" "23.txt"))
*SSS*
Break 21 [92]> (sort *sss* #'string-lessp)
("1.txt" "10.txt" "12.txt" "13.txt" "2.txt" "23.txt")
Break 21 [92]>

不幸的是,上面的代码不起作用。

谁能帮我得到一个自然排序函数?

最佳答案

这是一个通用的string-natural-lessp:

(defun string-natural-lessp (string-a string-b
                             &key
                               (start-a 0)
                               (end-a (length string-a))
                               (start-b 0)
                               (end-b (length string-b)))
  (do ((a-index start-a)
       (b-index start-b))
      ((or (>= a-index end-a)
           (>= b-index end-b))
       (not (>= b-index end-b)))
    (multiple-value-bind (a-int a-pos)
        (parse-integer string-a
                       :start a-index
                       :junk-allowed t)
      (multiple-value-bind (b-int b-pos)
          (parse-integer string-b
                         :start b-index
                         :junk-allowed t)
        (if (and a-int b-int)
            (if (= a-int b-int)
                (setf a-index a-pos
                      b-index b-pos)
                (return-from string-natural-lessp (< a-int b-int)))
            (if (char-equal (aref string-a a-index)
                            (aref string-b b-index))
                (progn
                  (incf a-index)
                  (incf b-index))
                (return-from string-natural-lessp
                  (char-lessp (aref string-a a-index)
                              (aref string-b b-index)))))))))

关于lisp - 如何在普通的 lisp 中实现自然排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27307660/

相关文章:

lisp - 试图计算总工资

qt - Commonqt 无法在 OS X Lion 上使用 Clozure CL

lisp - 如何在 Lisp 中找到二维数组中元素的列索引?

exception - 在 Common Lisp 中安装条件处理器

syntax - Unusual Scheme `let`绑定(bind),什么是 `f`?

lisp - 什么时候在Lisp中使用'(或引用)?

clojure - 翻转 "->"语句中的参数

lisp - 从列表中删除元素

Lisp方法流程

lisp - 在 lisp SLIME 调试器中检查变量