Call function in another lisp file

前端 未结 4 1099
死守一世寂寞
死守一世寂寞 2021-01-15 17:26

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 ot

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-15 18:03

    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.

提交回复
热议问题