Lisp importing/loading file

断了今生、忘了曾经 提交于 2019-12-04 15:50:30

问题


Is there a way in Lisp to include code from other Lisp files?

For example, in C++ I can do this:

#include <iostream>
#include <string>
#include "myfile.h"
etc...

And in Python,

import antigravity
import my_module
etc...

Is there a Lisp equivalent?


回答1:


In addition to Rainer's answer:

If you want to load ASDF-systems, use:

(asdf:load-system 'my-system)

Some Lisp implementations (CCL for example) also allow using require for loading ASDF systems, but that functionality is implementation dependent. If you are using Slime, and want to load a system interactively, you can do so by typing ,l my-system at the Slime REPL.

Another thing I wanted to point out is that, unlike in Python, using require or load in CL does not have anything to do with packages (think "namespaces"). So, if the code you are loading or requiring lives in its own package, you will either have to use its exported symbols qualified (foo:bar), or include those packages in the package your code lives in ((defpackage my-package (:use cl package-you-want-to-use ...) ...)). This does not only differ from Python imports, but also from C preprocessor #includes, the latter being mere textual inclusions.




回答2:


Use standard functions like LOAD and COMPILE-FILE.

ANSI Common Lisp also has a function REQUIRE.

If you use as 'system' maintenance tool as an extension to Common Lisp, then you can also load or compile a whole 'system'. A 'system' is a collection of files and subsystems for which operations like load and compile are defined.



来源:https://stackoverflow.com/questions/5447071/lisp-importing-loading-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!