2

In C, we separate code into two entities: interface for the client and implementation to perform the work required by the interface. Interface is put in .h and implementaiton is put in .c. This is convenient if we only want to expose the interface, not the source code, and we don't have to rely on documentation to look up the interface, which can be performed by the interface.

How can I achieve the same thing with Lisp?

3 Answers 3

5

Header files are not a feature of Lisps as a rule. The closest analog you will find is the package.lisp in libraries which includes a DEFPACKAGE form listing the symbols exported from the namespace. That, however, is a convention, not a requirement.

3

The good way to understand Lisp from a C hacker's point of view is that the loading of Lisp files to make a program is analogous not so much to C compiling, as to the linking.

In a C based toolchain, the linker takes object files into memory, resolves symbols, and produces an executable image.

In Lisp, the Lisp system loads files, and produces an image (in memory). There is usually some mechanism for saving everything to create an executable file.

Dynamic loading is even more similar to Lisp: the way Firefox loads plugins, or the way the Linux kernel or the Apache web server loads modules.

ALl those technologies built around providing safety and versioning in dynamic loading, whether Microsoft's COM and it's IUnknown and QueryInterface, or the Linux kernel's symbol versioning, or the ELF shared library mechanism, are all basically a "Greenspunning" of the Lisp compile-file and load function and Lisp's binding of symbols to entities like classes, functions and variables.

References:

Evaluation and Compilation

http://www.lispworks.com/documentation/lw60/CLHS/Body/03_.htm

System Construction:

http://www.lispworks.com/documentation/lw60/CLHS/Body/24_.htm

3
  • Still, at the development time, I don't want to expose the source code. I want to hide it via compiled file, and expose the declaration only. Or it means, I have to load the compiled lisp file into my development environment (i.e. emacs, or REPL) and let it look up? Microsoft only exposes the interfaces, not the source. How can this be done in Lisp?
    – Amumu
    Mar 16, 2012 at 8:24
  • @Amumu: look into FASLs and the ASDF system. You should be able to do what amounts to loading a DLL with only its exported names exposed with compiled Lisp files. Mar 16, 2012 at 15:58
  • @Amumu: In Lisp, a compiled file is considered to be an alternative, optimized version of a source file which has nearly the same effect when loaded. The source file is not necessary for the compiled file to work. Lisp implementation support the model of compiling source on the development system, and deploying executable code to the targets.
    – Kaz
    Mar 16, 2012 at 19:40
1

Why calculating is better then scheming. Languages like ocaml give ml and mli for the same purpose, plus you get type safety.

4
  • Yes, but in those languages, you get into a tizzy trying to do simple things like putting objects of a different type into a list. Also, they do not support the re-loading of modules at run time, never mind dealing gracefully with the versioning of functions and types which that may entail.
    – Kaz
    Mar 16, 2012 at 8:01
  • While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. Mar 16, 2012 at 12:03
  • Erlang does, kaz. It allows any types in lists and runtime reloading. Erlang also requires a list of exports at the top of the module to get a better idea of the interface it provides. Different types for lists isn't a problem in ocaml or haskell because you can create parameterized types anyway. Mar 16, 2012 at 15:21
  • 2
    Anyway, this is tagged [lisp] and so promoting non-Lisp is off topic and basically trolling. You know where to find Usenet if you need it (and I'm there).
    – Kaz
    Mar 16, 2012 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.