I. Creating a sample program Hello World
1. Write the asdf file hello.asd
[Plain] view plaincopy
<span style= "FONT-SIZE:18PX;" > (Defpackage:hello-system (: Use #:asdf #:cl))
(In-package:hello-system)
(Defsystem Hello
: Name "Hello World"
: Version "0.1"
: Author "CQ"
:d epends-on ()
: Components ((: File "package")
(: File "Hello":d epends-on ("package")))) </span>
This is ASD, which compiles and loads Lisp dependencies.
Defpackage defines a system's package name Hello-sytem, which is integrated from the following three packages.
In-package Import Package, if you want to use a package, you must import this package with In-package.
The next Defsystem macro defines the code structure for the entire project, along with some useless additional information. The important part is component, which defines two explicitly dependent source code files Package.lisp, and Hello.lisp, in general, requires at least three code files for a slightly larger formal Lisp program: one to define the package, A storage configuration information (omitted here), a real business logic code. If this project relies on other ASDF format Lisp packages, then write them in the depends-on section.
2. package definition file Package.lisp
[Plain] view plaincopy
(In-package:hello-system)
(Defpackage Hello
(: Nicknames Hello)
(: Use #:cl)
(: Export main *default-name*))
Defines a package named: Hello, and then uses the use segment to set this package to access all standard Common Lisp symbols, according to the Lisp standard they are located in the Common-lisp bag, the nickname of this package is CL. Finally I exported two of the symbols in the Hello package as an external interface.
3. Implement file Hello.lisp
[Plain] view plaincopy
(In-package:hello)
(DefVar *default-name* "World")
(Defun main (args)
(if (null args)
(Format t "Hello ~a~%" *default-name*)
(Hello args)))
(Defun hello (names)
(When names
(Format t "Hello ~a~%" (car names))
(Hello (Cdr names))))
Defines two functions, main and Hello
There are two function definitions in the code above, the main function is the entry of the entire program, the entry parameter is a list, if the list is empty, the default output is generated and the program ends, or another function hello is called to actually produce the output for each list element, note that this function I used the tail recursion, This is a very natural programming style in the Lisp program, completely without any performance degradation and saving explicit loop variables compared to the loop structure.
Two. Compiling and loading Lisp files
1, each module must have a description file, MODULE-NAME.ASD (in this case, HELLO.ASD). The file declares the module name and the list of files that make up the module. You can describe the dependencies between Lisp files, or you can describe dependencies between modules. ASD file, similar to the VC's engineering file, similar to the make file.
Method 1 for loading modules:
(Push #p "example/" asdf:*central-registry*): Add the directory where the ASD file resides
(Asdf:load-system "Module-name"): Load a module (in this case hello)
Method 2 for loading modules:
(Load "EXAMPLE/MODULE-NAME.ASD"): reads the contents of the ASD file without loading the module ()
(Asdf:load-system "Module-name"): Load a module
Three. Test code
[Plain] view plaincopy
Cl-user: (Hello:main nil)
Hello World
NIL
Cl-user: (Hello:main ' ("A" "NetEase" "sa")
Hello a
Hello NetEase
Hello SA
NIL
Hello is the package name, and main is the name of the function
You can also use In-package to import the package first, and enter the function name directly.
Source:
http://download.csdn.net/detail/cq20110310/4988409
Reference article Address
1.http://tianchunbinghe.blog.163.com/blog/static/7001200692314249376/
2.http://blog.csdn.net/dragonzht/article/details/8299539
Use ASDF to organize Lisp program compilation and loading