当在Guile的列表中排序!`'时,项目会消失

时间:2015-02-10 06:31:15

标签: sorting scheme guile

在字符串列表上调用sort!后,我似乎有一个消失项目的案例:

(我从我正在处理的Autogen模板中派生了这段代码。)

(use-modules (ice-9 pretty-print))

(let ((files '("log4cplus/socketappender.h"
              "log4cplus/qt4debugappender.h"
              "log4cplus/nteventlogappender.h"
              "log4cplus/ndc.h"
              "log4cplus/msttsappender.h"
              "log4cplus/mdc.h"
              "log4cplus/loglevel.h"
              "log4cplus/loggingmacros.h"
              "log4cplus/logger.h"
              "log4cplus/log4judpappender.h"
              "log4cplus/log4cplus.h"
              "log4cplus/layout.h"
              "log4cplus/hierarchylocker.h"
              "log4cplus/hierarchy.h"
              "log4cplus/fstreams.h"
              "log4cplus/clogger.h"
              "log4cplus/asyncappender.h"
              "log4cplus/version.h"
              "log4cplus/tstring.h"
              "log4cplus/qt5debugappender.h"
              "log4cplus/nullappender.h"
              "log4cplus/fileappender.h"
              "log4cplus/consoleappender.h"
              "log4cplus/clfsappender.h"
              "log4cplus/appender.h"
              "log4cplus/win32debugappender.h"
              "log4cplus/win32consoleappender.h"
              "log4cplus/tracelogger.h"
              "log4cplus/tchar.h"
              "log4cplus/syslogappender.h"
              "log4cplus/initializer.h"
              "log4cplus/configurator.h"
              "log4cplus/config.hxx"
              "log4cplus/thread/threads.h"
              "log4cplus/thread/syncprims.h"
              "log4cplus/thread/syncprims-pub-impl.h"
              "log4cplus/thread/impl/tls.h"
              "log4cplus/thread/impl/threads-impl.h"
              "log4cplus/thread/impl/syncprims-pmsm.h"
              "log4cplus/thread/impl/syncprims-impl.h"
              "log4cplus/thread/impl/syncprims-cxx11.h"
              "log4cplus/spi/rootlogger.h"
              "log4cplus/spi/objectregistry.h"
              "log4cplus/spi/loggingevent.h"
              "log4cplus/spi/loggerimpl.h"
              "log4cplus/spi/loggerfactory.h"
              "log4cplus/spi/filter.h"
              "log4cplus/spi/factory.h"
              "log4cplus/spi/appenderattachable.h"
              "log4cplus/streams.h"
              "log4cplus/internal/socket.h"
              "log4cplus/internal/internal.h"
              "log4cplus/internal/env.h"
              "log4cplus/internal/cygwin-win32.h"
              "log4cplus/helpers/thread-config.h"
              "log4cplus/helpers/socket.h"
              "log4cplus/helpers/pointer.h"
              "log4cplus/helpers/lockfile.h"
              "log4cplus/helpers/connectorthread.h"
              "log4cplus/helpers/timehelper.h"
              "log4cplus/helpers/stringhelper.h"
              "log4cplus/helpers/socketbuffer.h"
              "log4cplus/helpers/snprintf.h"
              "log4cplus/helpers/queue.h"
              "log4cplus/helpers/property.h"
              "log4cplus/helpers/loglog.h"
              "log4cplus/helpers/fileinfo.h"
              "log4cplus/helpers/appenderattachableimpl.h"
              "log4cplus/config/windowsh-inc.h"
              "log4cplus/config/win32.h"
              "log4cplus/config/macosx.h"
              "log4cplus/boost/deviceappender.hxx")))
  (begin
    ;; Add the generated header as it will not be found by file search.
    (append! files (list "log4cplus/config/defines.hxx"))
    (display "before sort:\n")
    (pretty-print files)
    (sort! files string<?)
    (display "after sort:\n")
    (pretty-print files)))

我看到的输出是:

before sort:
("log4cplus/socketappender.h"
 "log4cplus/qt4debugappender.h"
 "log4cplus/nteventlogappender.h"
 "log4cplus/ndc.h"
 "log4cplus/msttsappender.h"
 "log4cplus/mdc.h"
 "log4cplus/loglevel.h"
 "log4cplus/loggingmacros.h"
 "log4cplus/logger.h"
 "log4cplus/log4judpappender.h"
 "log4cplus/log4cplus.h"
 "log4cplus/layout.h"
 "log4cplus/hierarchylocker.h"
 "log4cplus/hierarchy.h"
 "log4cplus/fstreams.h"
 "log4cplus/clogger.h"
 "log4cplus/asyncappender.h"
 "log4cplus/version.h"
 "log4cplus/tstring.h"
 "log4cplus/qt5debugappender.h"
 "log4cplus/nullappender.h"
 "log4cplus/fileappender.h"
 "log4cplus/consoleappender.h"
 "log4cplus/clfsappender.h"
 "log4cplus/appender.h"
 "log4cplus/win32debugappender.h"
 "log4cplus/win32consoleappender.h"
 "log4cplus/tracelogger.h"
 "log4cplus/tchar.h"
 "log4cplus/syslogappender.h"
 "log4cplus/initializer.h"
 "log4cplus/configurator.h"
 "log4cplus/config.hxx"
 "log4cplus/thread/threads.h"
 "log4cplus/thread/syncprims.h"
 "log4cplus/thread/syncprims-pub-impl.h"
 "log4cplus/thread/impl/tls.h"
 "log4cplus/thread/impl/threads-impl.h"
 "log4cplus/thread/impl/syncprims-pmsm.h"
 "log4cplus/thread/impl/syncprims-impl.h"
 "log4cplus/thread/impl/syncprims-cxx11.h"
 "log4cplus/spi/rootlogger.h"
 "log4cplus/spi/objectregistry.h"
 "log4cplus/spi/loggingevent.h"
 "log4cplus/spi/loggerimpl.h"
 "log4cplus/spi/loggerfactory.h"
 "log4cplus/spi/filter.h"
 "log4cplus/spi/factory.h"
 "log4cplus/spi/appenderattachable.h"
 "log4cplus/streams.h"
 "log4cplus/internal/socket.h"
 "log4cplus/internal/internal.h"
 "log4cplus/internal/env.h"
 "log4cplus/internal/cygwin-win32.h"
 "log4cplus/helpers/thread-config.h"
 "log4cplus/helpers/socket.h"
 "log4cplus/helpers/pointer.h"
 "log4cplus/helpers/lockfile.h"
 "log4cplus/helpers/connectorthread.h"
 "log4cplus/helpers/timehelper.h"
 "log4cplus/helpers/stringhelper.h"
 "log4cplus/helpers/socketbuffer.h"
 "log4cplus/helpers/snprintf.h"
 "log4cplus/helpers/queue.h"
 "log4cplus/helpers/property.h"
 "log4cplus/helpers/loglog.h"
 "log4cplus/helpers/fileinfo.h"
 "log4cplus/helpers/appenderattachableimpl.h"
 "log4cplus/config/windowsh-inc.h"
 "log4cplus/config/win32.h"
 "log4cplus/config/macosx.h"
 "log4cplus/boost/deviceappender.hxx"
 "log4cplus/config/defines.hxx")
after sort:
("log4cplus/qt4debugappender.h"
 "log4cplus/qt5debugappender.h"
 "log4cplus/socketappender.h"
 "log4cplus/spi/appenderattachable.h"
 "log4cplus/spi/factory.h"
 "log4cplus/spi/filter.h"
 "log4cplus/spi/loggerfactory.h"
 "log4cplus/spi/loggerimpl.h"
 "log4cplus/spi/loggingevent.h"
 "log4cplus/spi/objectregistry.h"
 "log4cplus/spi/rootlogger.h"
 "log4cplus/streams.h"
 "log4cplus/syslogappender.h"
 "log4cplus/tchar.h"
 "log4cplus/thread/impl/syncprims-cxx11.h"
 "log4cplus/thread/impl/syncprims-impl.h"
 "log4cplus/thread/impl/syncprims-pmsm.h"
 "log4cplus/thread/impl/threads-impl.h"
 "log4cplus/thread/impl/tls.h"
 "log4cplus/thread/syncprims-pub-impl.h"
 "log4cplus/thread/syncprims.h"
 "log4cplus/thread/threads.h"
 "log4cplus/tracelogger.h"
 "log4cplus/tstring.h"
 "log4cplus/version.h"
 "log4cplus/win32consoleappender.h"
 "log4cplus/win32debugappender.h")

正如您所看到的,后排序列表中缺少了一半的项目。

我在Scheme源中做错了什么?

更新

因此,通过反复试验,我偶然发现了一些似乎是修复的问题。当我将(sort! files string-ci<?)更改为(set! files (sort! files string<?))时,我会得到所需的结果。但我很困惑,为什么就地排序不能像我期望的那样工作。

1 个答案:

答案 0 :(得分:3)

从手册:

  

对序列项进行排序,可以是列表或向量。 less用于比较序列元素。排序是破坏性的, 表示输入序列被修改以产生排序结果 。这不是一个稳定的类型。

https://www.gnu.org/software/guile/manual/html_node/Sorting.html

该程序具有破坏性,但并不保证输入序列可用。因此,您始终需要将返回值用作排序序列。