Lisp Programming Question:
Download Questions PDF

How to determine if a file is a directory or not? How do I get the current directory name from within a Lisp program? Is there any way to create a directory?

Answer:

There is no portable way in Common Lisp of determining whether a file
is a directory or not. Calling DIRECTORY on the pathname will not
always work, since the directory could be empty. For UNIX systems
(defun DIRECTORY-P (pathname)
(probe-file (concatenate 'string pathname "/.")))
seems to work fairly reliably. (If "foo" is a directory, then "foo/."
will be a valid filename; if not, it will return NIL.) This won't, of
course, work on the Macintosh, or on other operating systems (e.g.,
MVS, CMS, ITS). On the Macintosh, use DIRECTORYP.

Moreover, some operating systems may not support the concept of
directories, or even of a file system. For example, recent work on
object-oriented technology considers files to be collections of
objects. Each type of collection defines a set of methods for reading
and writing the objects "stored" in the collection.


There's no standard function for finding the current directory from
within a Lisp program, since not all Lisp environments have the
concept of a current directory. Here are the commands from some Lisp
implementations:
Lucid: WORKING-DIRECTORY (which is also SETFable)
PWD and CD also work
Allegro: CURRENT-DIRECTORY (use excl:chdir to change it)
CMU CL: DEFAULT-DIRECTORY
LispWorks: LW:*CURRENT-WORKING-DIRECTORY*
(use LW:CHANGE-DIRECTORY to change it)

Allegro also uses the variable *default-pathname-defaults* to resolve
relative pathnames, maintaining it as the current working directory.
So evaluating (truename "./") in Allegro (and on certain other
systems) will return a pathname for the current directory. Likewise,
in some VMS systems evaluating (truename "[]") will return a pathname
for the current directory.

Download Lisp Programming Interview Questions And Answers PDF

Previous QuestionNext Question
Who is the founder of Lisp Programming?Explain What is the ouput like?