Use ASDF to compile and load LISP programs

Source: Internet
Author: User

1. Create the sample program Hello World


1. Write the ASDF file hello. ASD

(Defpackage: hello-system (: Use #: ASDF #: CL ))

(In-package: hello-System)

(Defsystem hello

: Name "Hello World"

: Version "0.1"

: Author "CQ"

: Depends-on ()

: Components (: file "package ")

(: File "hello": depends-on ("package "))))


This is the dependency between ASD compilation and lisp loading.

Defpackage defines the system package name hello-sytem, which is integrated from the following three packages.

To import an in-package, you must use in-package to import the package.

The following defsystem macro defines the code structure of the entire project and some useless additional information. The important part is components, which defines two source code files with clear dependencies package. LISP, and hello. LISP, in general, for a regular lisp program of a relatively large scale, requires at least three code files: one to define the package and the other to store the configuration information (which is omitted here ), put the actual business logic code. If this project depends on the lisp software package in other ASDF formats, write it in the depends-on section.


2. Package definition file package. LISP

(In-package: hello-System)

(Defpackage hello

(: Nicknames Hello)

(: Use #: CL)

(: Export main * default-name *))


Defines a package named: Hello, and 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 package, the nickname of this package is cl. Finally, I exported two symbols in the hello bag as an external interface.


3. Implement file hello. LISP

(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 ))))



Two functions are defined: Main and hello.

The code above contains two function definitions. The main function is the entry of the entire program, and the entry parameter is a list. If the list is empty, the default output is generated and the program ends, otherwise, another function hello will be called to generate the output for each list element. I noticed that this function uses tail recursion, which is a natural programming style in the lisp program, there is no performance loss at all and it saves explicit cyclic variables compared to the cyclic structure.


Ii. Compile and load lisp files

1. Each module has a description file, module-name.asd (hello. ASD in this example ). This file declares the module name and the list of files that constitute the module. It can describe the dependencies between lisp files or between modules. ASD files, similar to VC engineering files, and similar to make files.


Method 1 for Loading modules:


(Push # P "example/" ASDF: * Central-Registry *): add the directory where the ASD file is located.


(ASDF: Load-system "Module-name"): load a module (hello in this example)





Method 2 for loading a module:


(Load "Example/module-name.asd"): Read the content of the ASD file, do not load the module ()


(ASDF: Load-system "Module-name"): loads a module.


Iii. Test code

CL-USER: (Hello: Main nil)

Hello World

Nil

CL-USER: (Hello: main' ("A" "Netease" "sa "))

Hello

Hello Netease

Hello SA

Nil

Hello is the package name, main is the function name

You can also use in-package to import the package and enter the function name directly.


Source code:

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 compile and load LISP programs

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.