Use ASDF to organize Lisp program compilation and loading

Source: Internet
Author: User

I. Creating a sample program Hello World


1. Write the asdf file hello.asd

[Plain] view plaincopy

  1. <span style= "FONT-SIZE:18PX;" > (Defpackage:hello-system (: Use #:asdf #:cl))

  2. (In-package:hello-system)

  3. (Defsystem Hello

  4. : Name "Hello World"

  5. : Version "0.1"

  6. : Author "CQ"

  7. :d epends-on ()

  8. : Components ((: File "package")

  9. (: 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

  1. (In-package:hello-system)

  2. (Defpackage Hello

  3. (: Nicknames Hello)

  4. (: Use #:cl)

  5. (: 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

  1. (In-package:hello)

  2. (DefVar *default-name* "World")

  3. (Defun main (args)

  4. (if (null args)

  5. (Format t "Hello ~a~%" *default-name*)

  6. (Hello args)))

  7. (Defun hello (names)

  8. (When names

  9. (Format t "Hello ~a~%" (car names))

  10. (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

  1. Cl-user: (Hello:main nil)

  2. Hello World

  3. NIL

  4. Cl-user: (Hello:main ' ("A" "NetEase" "sa")

  5. Hello a

  6. Hello NetEase

  7. Hello SA

  8. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.