Вопрос

I'm having one little hiccup in understanding Lisp. According to the Common Lisp standard, a form is an atom or list that is meant to be evaluated. That seems easy enough.

In the real world, at the moment, we store programs in files. SBCL, Clojure, and the like all have the notion of a file that can also be evaluated.

How is this understood? Is a file treated ultimately like a single form to be evaluated, is it a collection of forms? For some reason, this has been really confusing me.

Это было полезно?

Решение

According to the CLHS:

load sequentially executes each form it encounters in the file
named by filespec

which means that a file is a sequence of forms.

Другие советы

A file is a place to store forms. Opening the file (with OPEN or WITH-OPEN-FILE) provides a stream, and the READ function can read forms from the stream into objects that can be evaluated or compiled.

In Common Lisp, it's important to realize that the semantics of the program are defined on the objects that result from reading, not the text that is stored in the file.

http://l1sp.org/cl/3.2.3 might help understand it from a compilation perspective.

http://l1sp.org/cl/24.1.1 talks about load.

I'm having one little hiccup in understanding Lisp. According to the Common Lisp standard, a form is an atom or list that is meant to be evaluated. That seems easy enough.

Notice that this is basically data based. EVAL takes a form as data and not text - as its argument to evaluate. We have to read the text first to create a form for EVAL. If we talk about forms in a file, this is basically a short hand for: forms read from the file.

In the real world, at the moment, we store programs in files. SBCL, Clojure, and the like all have the notion of a file that can also be evaluated.

Common Lisp is a part of the real world. Code is mostly stored in text files. Not necessarily so, but his is how it is. That's why Common Lisp has functions like OPEN, WITH-OPEN-FILE, READ, LOAD, COMPILE-FILE and more.

No, files in Lisp can not be 'evaluated'. EVAL is the evaluator and it takes a Lisp form as an argument, not a file. In Lisp a file can be loaded. Loading a file means this: opening the file for reading, reading from the file and evaluating form by form.

In Common Lisp files can also be compiled to create a 'fasl' (fast load) file. With something like SBCL, this file will contain machine code and data. This file can also be loaded. Loading will execute the generated code for each form, one by one.

So, Common Lisp can load two types of files: text files with Lisp code and FASL files with compiled code.

You can also open a text file for reading, use the Lisp reader to read the forms and do what ever you want with the results. You can also open a text file for writing and write Lisp forms as text to that file.

Storing source code in files is common practice in Lisp.

There are exceptions to this:

  • most Common Lisp implementations (not all) allow us to save an image. The image is a dump of the current memory state of a running Lisp systems. Thus you can have Lisp forms as data in your running program, then dump the the image and later restart the image. The restarted image will still have all data, which means the code stored as data will also still be there.

  • Something like Interlisp-D, a no longer widely used Lisp system, had also a Common Lisp implementation. Interlisp-D stored source code in text files under program control. Thus the text files were more used like a data base.

  • A few Lisp systems also support auto-loading. Call a function, the function is not known, then it looks somewhere else (a database, source code) to locate the source code, loads it and it then retries to call the now known function. This way it is not necessary for the user to load the files, the Lisp system does it in the background. Auto-loading was used in early Lisp systems to save space in main memory and not load everything at that. Even newer Lisp implementations may use some form of auto-loading for some functionality.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top