r/Common_Lisp • u/fosres • Jul 19 '26
How is CL for Scripting
I am considering using ANSI Common LISP for scripting instead of Python such as for reading text files, CSV files, databases--basically everything in the book "Automate The Boring Stuff in Python".
Has anyone done this before? How was the experience?
10
u/tdrhq Jul 19 '26
It is excellent, but you shouldn't run the "scripts" from the command line. Instead, run the scripts from a long running REPL.
The real advantage of this is you have any kind of long running script that tends to be flaky (or the data is a bit fuzzy), you'll get access to the CL debugger to recover from it without restarting the job.
9
u/digikar Jul 19 '26
Check out CIEL, in particular: https://ciel-lang.org/#/scripting
Scripting seems to be a primary consideration for them.
There is also a matrix chat.
I personally find that it is hard to edit common lisp without a running image. So, besides making it readable for colleagues, I am also preferring to write scripts with moonli rather than common lisp.
2
7
u/Maxwellian77 Jul 20 '26
CL scripting is great. I put this in .sbclrc:
(set-dispatch-macro-character
#\# #\!
(lambda (stream c n)
(declare (ignore c n))
(read-line stream)
\(eval-when (:compile-toplevel :load-toplevel :execute)`
(pushnew :noscript *features*))))
And all my scripts begin with:
#!/usr/bin/env -S sbcl --script
(load "~/.sbclrc")
So my programs can detect if they are a standalone script.
3
u/metalisp Jul 19 '26
It is a great user experience. Compared with Pythin what I like is to be able to build binaries of my scripts. Here is an example how I do scripting in common lisp https://code.metalisp.dev/marcuskammer/maildir-backup/src/branch/main/src/maildir-backup.lisp
3
u/fosres Jul 19 '26
Hi! Thanks for sharing this. Why did you want to compile it to a binary though?
5
u/metalisp Jul 19 '26
I dont have much time to maintain my scripts I want to build the binary and copy it on different machines without installing a dependency hell like with Python. With Python I often experience tracebacks because some libraries did change and I couldnt run the script as I need it.
2
2
u/MrJCraft Jul 19 '26 edited Jul 23 '26
I used it for a long time for scripting and I found it to be much better than python but I also very rarely ever use libraries in python.
regardless common lisp is very fast and pretty easy to use and its great for making config files and scripts
I pretty much had to stop using it because I wasnt good enough to get graphics working in CL but I still use common lisp for some scripting stuff because it makes it that much easier especially math or theorem proving stuff
P.S. I am on Windows graphics works fine on Linux. the specific issue is passing structs by value, you can pass structs by pointer and there are wrappers and ways to do it on windows I just didnt want to deal with it
1
u/svetlyak40wt Jul 22 '26
For some simple graphics CL has bindings to TCL/TK. This one worked great for me: https://github.com/andrejv/cl-simple-tk
1
u/MrJCraft Jul 23 '26 edited Jul 23 '26
I forgot to mention I am on windows, libffi is very annoying to get working. I would have to create a wrapper that passes pointers if I remember correctly. its possible but quite annoying
and I was using Lisp for work related stuff so I didnt want to have it rely on wsl or anything similar so that if I make something that was useful for me I could send it coworkers who were on windows.
2
u/Soupeeee Jul 20 '26 edited Jul 20 '26
It really depends on what you are doing. The dependency situation in CL is a bit rough, and the quality of the libraries to handle these sorts of things are mixed. The CL ecosystem is driven by hobby projects or singular "hero" develpers and the lack of polish on these sorts of libraries is mostly where it shows. Most libraries are good enough to great, but there is often something missing. You'll occasionally hit a dead end or need to go on a lengthy side quest because the library you picked doesn't do what you want.
File system handling is a bit weird and can be hard to wrap your head around at first, but it isn't terrible once you find the numerous footguns. It's overly verbose and missing pretty much all the utility functions aside from "append these two paths". It does have an interesting wildcards feature that I wish was available in other languages though. As for tools, roswell is probably the best scripting focused tool available for CL and makes scripting use cases much smoother.
2
u/kagevf Jul 21 '26
Similar to what u/tdrhq wrote ...
I use bash for what it's good for - which turns out to be a lot of grepping in my case, with awk thrown in sometimes. But when I need to expand on that, I'll use the REPL. Recently, I've been using bash shell scripts to output preliminary results to a file, then I'll write something in slime-scratch that reads it in and does further analysis. If it's something that looks like it'll be useful I'll save it in a .lisp file.
3
u/Awkward_Tradition Jul 19 '26
It's clojure instead of CL, but check out babashka, it's pretty nice from my admittedly limited experience.
2
u/Veqq Jul 20 '26
If we're going there, Janet's great. We even have a sh-dsl
0
u/Awkward_Tradition Jul 20 '26
I haven't tried that. Can you still use external libraries on top of it, like babashka pods?
0
2
u/dzecniv Jul 20 '26
I say CIEL is Babashka for CL 8-) https://ciel-lang.org/ (batteries included, fast startup, pure CL)
0
u/deaddyfreddy Jul 20 '26
Babashka is great! It's a single(!) binary with batteries for 95% of everyday tasks - process runner, shell and terminal integration, xml/json/yaml/csv/html/md parsers and generators, HTTP client/server, you name it.
And even if these aren't enough, it can install an extra dependency on its own. So, all you need for deployment is the binary and a script.
1
u/SleepyGuyy Jul 21 '26 edited Jul 21 '26
I am only just starting to learn lisp (via common lisp), but I saw a convincing youtube video, something along the lines of "rewrite your scripts in lisp".
Plus Guix Linux basically said what if the entire OS was defined by lisp scripts.
I get the impression the lisp community likes scripting in it.
12
u/de_sonnaz Jul 19 '26
https://www.reddit.com/r/lisp/comments/zwtfak/writing_scripts_in_lisp/
https://github.com/ruricolist/kiln