I recently discovered a strange behaviour of racket: Whenever I try to filter a list of directories created via directory-list
my REPL returns me an empty list, but when I try the same with an quasiquoted list my REPL returns a correctly filtered list.
My questions is now: Why it's impossible to filter a list of directories via directory-exists?
and how it's possible to list only directories in racket?
Some examples:
When I use directory-exists?
in combination with filter
and directory-list
the REPL returns me everytime an empty list:
(filter directory-exists? (map path->complete-path
(directory-list (expand-user-path "~"))))
;; '()
Now, when I filter an quasiquoted list like in the following example. The REPL returns a correct filtered list, with all existing directories (without the imaginary directory ~/blablubb
)
(filter directory-exists?
(map path->complete-path `(,(path->complete-path (expand-user-path "~/documents"))
,(expand-user-path "~/blablubb" ))))
;; '(#<path:/home/niklas/documents>)
directory-list
returns relative paths by default, which are later implicitly resolved with respect to the(current-directory)
, which might be different from the directory you passed todirectory-list
.Kind of like this:
From the current directory, the file is at path
dir/file1
, not justfile1
.One solution is to ask
directory-list
to include the directory in the paths returned:Then if
the-dir
is a complete path, the results will also be complete paths.