Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I go about splitting Lisp code into multiple source files?

Tags:

Right now, everything I do manages to fit in a single source file, and a surprisingly small one at that. How do you decide how much and what to split off into separate files?

With Java, it's easy to decide what goes in one file (the decision is made for you already), but in Lisp I find I write many small functions that build on one another and it becomes hard to decide what, if anything should be split out. As I tackle larger projects in Lisp, it would be nice not to have to reinvent the wheel, but I can't find a lot of concrete info on the web about this.

Can you share some strategies for dealing with larger projects in Lisp, or point me to some resources that deal with this?

like image 948
Galghamon Avatar asked Jul 24 '09 19:07

Galghamon


2 Answers

When you start writing a Lisp program then it might be useful to start with a single file. Once the code gets too large (whatever that is) you can split it up. When you are approaching something that needs organization, then you should invest some work into it.

A few hints:

  • There are a few tools to manage source file dependencies and provide actions like compile, load, compile-and-load and others. ASDF is one, but there are also others.

  • You need one file to describe the dependencies. Name it so that it can be recognized.

  • You might need a file to define one or more packages.

  • You might need put implementation specific functionalities in their own files.

  • move larger amounts of configuration data to their own files

  • general utilities should be in another file

  • macros should be defined before used. Other files depend on this file and should be recompiled automatically if the macro definition is changed.

  • group functionality together into a file if it is logically connected. In a drawing program: all drawing functions, all user interface commands, saving data to files, printing, ...

  • don't care too much about the file size. Lisp source files can be large. Sometimes 100k.

  • moving around in the files is supported by the development environment. M-. on a symbol finds its source.

  • make sure that you can reload a file, without the need to restart the whole Lisp.

  • Common Lisp provides LOAD and COMPILE-FILE as functions. You can use these functions in your own files.

like image 109
Rainer Joswig Avatar answered Sep 28 '22 04:09

Rainer Joswig


See "Making a small Common Lisp project" by Zach Beane. He has an updated post at Making a small Lisp project with quickproject and Quicklisp

like image 36
huaiyuan Avatar answered Sep 28 '22 05:09

huaiyuan