在另一个lisp文件中调用函数 [英] Call function in another lisp file

查看:246
本文介绍了在另一个lisp文件中调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在Lisp里写一个游戏。为了清楚起见,我想将代码分成不同的.lisp文件。



如何从其他文件中的函数调用函数? / p>

例如file1.lisp有一个名为function1的函数,file2.lisp有一个名为function2的函数。
我如何调用function2从function1?



谢谢!

解决方案

只要你知道,有各种不同的Lisp系统。我会发布Common Lisp的答案。



天真的方法是使用(加载filename.lisp),但这并没有真正的工作很久以后因此...



Common Lisp有一个名为ASDF的库,用于处理包装和文件管理。 ASDF有一些设置。


  1. 创建ASDF查找文件的目录。

  2. 将此信息添加到我的Lisp系统的init文件

我使用这个在我的 .sbclrc 文件(假设我创建一个.asdf文件在〜):

 (pushnew〜/ .asdf /asdf:* central-registry * #等于

我通常使用以前构建的ASDF文件,然后修改它。



以下是ASDF文件的示例内容:

 (asdf:defsystem#:cl- linq 
:depend-on(#:alexandria#:aaphora)
:components((:filecl-linq))
:namecl-linq
:版本0.1
:维护者保罗·内森
:作者保罗·内森
:许可证LLGPL
:描述CL LINQ风格的接口与SQL的压力
:long-description
用于在SQL / LINQ样式
语法中管理和查询数据集的DSL cl-linq提供了一个简单可用的primitiv集es到
使数据检查简单直观。 )

我把这段代码放在一个文件中 cl-linq.asd 我的源代码旁边( cl-linq.lisp 从组件cl-linq在defsystem中),然后将 cl-linq.asd 文件符号链接到我的〜/ .asdf / 目录。



在我的cl-linq.lisp文件中,我包括:

 (defpackage:cl-linq 
(:use
:common-lisp
:anaphora)
(:export
#:query
# -linq-select))
(in-package:cl-linq)

你的情况下,我会有2个组件;每个组件都有自己的defpackage表单,导出另一个软件包所需的功能。



对于这些例子,我采取了 CL-LINQ 的代码,我的一个项目,您可以自由地将其用作模板。


I have to write a game in Lisp. In order to make it clear, I wanted to split the code in different .lisp files.

How can I call a function out of a function in the other file?

E.g. file1.lisp has a function called function1 and file2.lisp has a function called function2. How can I call function2 out of function1?

Thanks!

解决方案

Just so you know, there are a variety of different Lisp systems. I'll post the answer for Common Lisp.

The naive way is to use (load "filename.lisp"), but that doesn't really work very well after a while. Therefore...

Common Lisp has a library called "ASDF", which handles packaging and file management. There's a bit of setup to ASDF.

  1. Create directory where ASDF looks for files.
  2. Add this information to my Lisp system's init file.

I use this in my .sbclrc file (assuming that I created a .asdf file in ~) :

(pushnew "~/.asdf/" asdf:*central-registry* :test #'equal)

I usually use a previously built ASDF file and then modify it.

Here's a sample ASDF file's contents:

(asdf:defsystem #:cl-linq
  :depends-on ( #:alexandria #:anaphora)
  :components ((:file "cl-linq"))
  :name "cl-linq"
  :version "0.1"
  :maintainer "Paul Nathan"
  :author "Paul Nathan"
  :licence "LLGPL"
  :description "CL LINQ style interface with strains of SQL"
  :long-description
  "DSL for managing and querying datasets in a SQL/LINQ style
syntax. cl-linq provides a simple and usable set of primitives to
make data examination straightforward. ")

I put this code in a file cl-linq.asd next to my source code (cl-linq.lisp from the component "cl-linq" in the defsystem) and then symlink the cl-linq.asd file to my ~/.asdf/ directory.

Within my cl-linq.lisp file I include this:

(defpackage :cl-linq
  (:use
   :common-lisp
   :anaphora)
  (:export
   #:query
   #:cl-linq-select))
(in-package :cl-linq)

So for your case, I would have 2 components; each with their own defpackage form, exporting the functions out that the other package needed.

For the examples, I've taken the code from CL-LINQ, a project of mine. You are quite free to use it as a template.

这篇关于在另一个lisp文件中调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆