80

I am a C++ programmer trying to learn Common Lisp. I have looked at some books like Land of Lisp and read numerous online articles about the various virtues of Lisp. However, I need some advice.

Almost everything I have read about Common Lisp has to do with how amazing it is and how amazingly fast you can get stuff done with it and how it amazingly solved many problems with modern programming languages 30 years ago. Also how amazing macros are, and how every every programming paradigm (OO, functional, actor based or whatever, etc) can be used in Lisp, and how lists are the ultimate data structure. Basically treating Lisp like a research language and saying how different and revolutionary it is.

And all that stuff is probably true, but the problem is I haven't seen much stuff how to do practical things like read a file and split it into words and do some processing on it. I'm not interested in learning Common Lisp for the sake of learning Common Lisp, but for the sake of getting thing that I used to do in C++ done faster and with fewer errors.

So my question is what is the best resource (be it a website, book, anything) that focuses on teaching how to use Common Lisp to do common programming tasks like

  • How to read files
  • How to read a file, replace words in the file, and write the result back to the file
  • Iterate the files in a directory and other filesystem stuff
  • Interact with an SQL db
  • Do communications over sockets
  • Threading for stuff like a webserver
  • Create GUIs
  • Perform operations on binary files
  • Write a parser (not an interpreter for Lisp in Lisp, which as I understand is like 5 lines of Lisp)
  • Interact with the operating system (i.e. stuff written in C or C++) to do stuff Lisp can't do natively
  • How to write Lisp extensions in C (is that possible?)
  • Embed a lua interpreter (is that possible?)

And also on a less immediately practical note, how to implement common data structures in lisp such as an heap, stack, binary search tree, etc. However that may be just using Lisp's list operations like car and cdr in the right way. I don't know.

I highly doubt that any of this (with the unlikely exception of the last two in the list) is impossible with Lisp or people wouldn't love it so much. And the aforementioned stuff that I've read mentions plenty of real world software written in Lisp (Yahoo! web store comes to mind).

However, having programming in a (the?) imperative language before, I am anxious to get to using what new knowledge I get to write real-world applications. So what's the quickest way to learn writing practical software with Lisp?

By the way, I have seen Peter Seibel's Practical Common Lisp but, judging by the TOC, it only touches on some of the things I would like to learn to use Lisp to do.

One more question if I may (sorry if this is combining two questions into one), where can I find a reference to Lisp's functions and stuff?

And I really want to like Lisp.

Dan Rigby
  • 17,133
  • 6
  • 43
  • 60
NewLisper
  • 835
  • 1
  • 7
  • 5
  • 3
    Nice question. I myself, being a C++ programmer, have been learning Lisp this summer, mainly with the books proposed in the answers. Very happy till now, but still getting my way through macros. On Lisp by Paul Graham is a real jewel. Also, the material on Emacs Lisp helps you to keep in track with Lisp in general, as well as allows you to write real useful scripts from within the editor itself. – Diego Sevilla Aug 30 '11 at 22:25
  • There is now an extensive [Common Lisp Resources](http://stackoverflow.com/documentation/common-lisp/534/getting-started-with-common-lisp/27861/common-lisp-learning-resources) page on the Getting Started page of Common Lisp on Docs.SO. – Gustav Bertram Apr 24 '17 at 10:41
  • There is also now an up to date [Common Lisp Cookbook](https://github.com/LispCookbook/cl-cookbook). Its TOC isn't that extensive yet though, so we search *awesome* and *modern* libraries in the [Awesome CL](https://github.com/CodyReichert/awesome-cl) list. And +1 for E. Weitz' "CL Recipes". – Ehvince Jul 23 '17 at 16:24

2 Answers2

97

I would propose reading 'Practical Common Lisp', since it already answers some of your questions.

There are probably three to four books you should read:

Common Lisp Reference

Manuals

Now the next thing you should check out is the manual of your Lisp implementation. It describes a lot of specific extensions: networking, threads, ...

Documentation for Common Lisp implementations:

SLIME (the Emacs-based Lisp-IDE) has a SLIME User Manual.

Documentation for Common Lisp libraries:

Libraries

For libraries use

Now looking at some of your points:

  • How to read files

See the files and streams dictionary in the HyperSpec. WITH-OPEN-STREAM, READ, READ-LINE, READ-CHAR, READ-BYTE, READ-SEQUENCE, ...

  • How to read a file, replace words in the file, and write the result back to the file

Use above. See also: WRITE and related.

  • Iterate the files in a directory and other filesystem stuff

See above. DIRECTORY, pathnames, ...

  • Interact with an SQL db

Use for example the CLSQL library.

  • Do communications over sockets

See the manual of your Lisp or use one of the portable libraries. See Quicklisp.

  • Threading for stuff like a webserver

See the manual of your Lisp or use one of the portable libraries. See Quicklisp.

  • Create GUIs

Depends. See Quicklisp or an implementation specific library.

  • Perform operations on binary files

See Hyperspec for file and stream operations. WRITE-BYTE, READ-BYTE. Open a stream as a binary stream.

  • Write a parser (not an interpreter for Lisp in Lisp, which as I understand is like 5 lines of Lisp)

Use one of the existing tools for that. Study existing parsers. There are many parsers written in Lisp, but not much in books about that (other than natural language parsers, which are described in the AI literature).

  • Interact with the operating system (i.e. stuff written in C or C++) to do stuff Lisp can't do natively

Depends. See Quicklisp or an implementation specific library.

  • How to write Lisp extensions in C (is that possible?)

Depends. See Quicklisp or an implementation specific library. -> FFI

Final advice: Read code from other authors.

Study other Lisp code. There is enough very diverse Lisp code out there. From web servers to music composition software.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
  • 2
    Seems like a lot of stuff is implementation specific. Is there anything like C which is not _implementation_ specific but _operating system_ specific? Seems like it's unfeasible to make everyone who wants your code use the implementation you picked. Also I'm not really interested in AI. – NewLisper Aug 29 '11 at 00:21
  • 4
    @NewLisper: there are a lot of portability libraries. See Quicklisp. – Rainer Joswig Aug 29 '11 at 00:25
  • 4
    @NewLisper: even if you are not interested in AI, you should read some of the literature, because it describes the 'symbolic programming' approach for which Lisp was invented. – Rainer Joswig Aug 29 '11 at 00:27
  • 2
    @NewLisper: This is a pretty complete answer. Lisp is closer to Python in how it relates to the OS & C/C++ calls. It's said to be pretty straightforward to put together a FFI (foreign function interface). It's also worth noting that it seems that the preponderance of Common Lisp users on the web seem to be using SBCL. – Paul Nathan Aug 29 '11 at 18:04
  • 1
    I think [On Lisp](http://www.paulgraham.com/onlisp.html) by Paul Graham (available as a free download) is one of the best Lisp books available. It shows the power of Lisp in general and macros in particular. It probably doesn't answer any of the specific questions you have, so don't start with it, but you might want to look at it after you've answered those questions. If you understand once-only, you are a master of macros. – Liam Aug 30 '11 at 04:58
  • 2
    I would add some concrete libraries: sockets: usocket; webserver: hunchentoot; threading: bordeaux-threads, chanl; OS-interaction/linking C-stuff: CFFI, UFFI. – Svante Aug 30 '11 at 08:36
  • 1
    Coming from C++, lisp is quite a different beast, but if you think of languages such as Python or Ruby, the way libraries are handled, for example, shouldn't be such a shock. – finitud Dec 18 '12 at 18:25
  • @RainerJoswig: You have helped me so many times in learning Lisp, and your answers are always great, hence I awarded the bounty to your answer, and basically I just wanted to say: "Thank you" :-). – Golo Roden Apr 28 '15 at 20:55
  • 2
    The book "Common Lisp Recipes" by Edmund Weitz sounds like it might be exactly what the original poster was looking for. http://weitz.de/cl-recipes/ – Greg Buchholz Apr 22 '16 at 17:37
  • @GregBuchholz: yes, thanks for mentioning it! It's newer than my original answer. I've added it above to the list of books. – Rainer Joswig Apr 22 '16 at 17:41
6

Check out Cliki the Common Lisp wiki it provides a list of libraries available for Common Lisp which will help you accomplish all your items.

Also, you're going to want to check out the Common Lisp Cookbook (there's also a more updated version). It has a bunch of code for common tasks such as reading a file one line at a time, and Foreign Function Interfaces for interacting with libraries written in C.

You can write extensions for Lisp in C depending on which implementation you're using. Emacs-Lisp for example allows you to do that though it isn't Common Lisp. Usually what you want to do is write the code in Common Lisp and then optimize it as much as possible using different Lisp compiler declarations, or the other method where you use a foreign function interface.

Threading depends on which implementation you use, but I think most of them have threads now.

Hunchentoot is one of the best Lisp web servers and is pretty easy to get started with. You don't have to write any threading code yourself, you just have to write the HTTP request handler functions.

Someone compiled a list of GUI options for Lisp:

  • cl-gtk2, an interface to the GTK gui library
  • McClim
  • Garnet
  • Common Qt
  • EQL
  • 1
    Common Lisp Cookbook looks really useful. Like I said to the other guy, it seems like a lot of stuff is implementation specific. It doesn't seem fair to make everyone you want to be able to run your code use the same implementation you use. Is that just the way it is? – NewLisper Aug 29 '11 at 00:27
  • Yeah that's how it is, some people try and stick to the ANSI Common Lisp standard though or try to stick with libraries that work across multiple implementations so it isn't too much of a hassle. –  Aug 29 '11 at 00:30
  • Do you lose the ability to do a lot of things if you stick to ANSI Common Lisp, like filesystem stuff, networking stuff, etc? – NewLisper Aug 29 '11 at 00:41
  • Lisp makes it easy to write portable libraries on top of implementation specific APIs, so I wouldn't worry about it. – Pavel Penev Aug 29 '11 at 06:55
  • Hey, would you mind changing the url of the Cookbook to https://lispcookbook.github.io/cl-cookbook ? It's the same content, but largely augmented, the outdated stuff removed, and in a better looking shape. See its git history ! Thanks. – Ehvince Sep 18 '17 at 16:28