lisp是一门强大的语言无可否认,但有时候lisp还是免不了很二,比如:遍历目录!
不管是用C还是python想要遍历指定的目录都很简单,而且跨平台也很容易;但对于lisp如果你想遍历一个目录下的所有文件,情况则没那么简单。首先你得清楚路径名的概念,然后,对于不同的lisp实现你得编写不同的代码,最后,你还得考虑windows与linux或者其他操作系统在目录结构上的差异。总之,如果你一直很懒,一直都不曾编写通用的、跨平台的目录遍历函数,那么每当你有遍历目录的需求时,你都会很纠结。
lisp下遍历目录的代码:
;;;;directory operation
(defun component-present-p (value)
(and value (not (eql value :unspecific))))
(defun directory-pathname-p (p)
(and
(not (component-present-p (pathname-name p)))
(not (component-present-p (pathname-type p)))
p))
(defun pathname-as-directory (name)
(let ((pathname (pathname name)))
(when (wild-pathname-p pathname)
(error "Can't reliably convert wild pathnames."))
(if (not (directory-pathname-p name))
(make-pathname
:directory (append (or (pathname-directory pathname) (list :rela