Simping for Common Lisp
Another post about praising common lisp qualities
Tags: programming, opinion, talk, common lisp
MACROS
That is the answer, simple and direct. Macros are the greatest tool regarding common lisp, it gives us more flexibility but also privacy.
What do I mean by both terms ‘privacy’ and ‘flexibility’?
Privacy because it can hide functions in the package, you don’t need to expose your functions to make it work, just as you export a class with private methods, with macros you can expose simply an interface that will call those functions internally and be nice syntatically.
Regarding flexibility it is also pretty direct, you don’t need to call functions everytime, you can make an structure that organizes the code itself with functions and other structures internally so you have a more coherent design and a DSL if you prefer.
Imagine you have a module to graphics that you want to expose to the public, if it was in C you would create a header files with all the functions available there, but in Common Lisp you can create a macro that can be like:
(draw-line :x1 0 :y1 120 :x2 100 :y2 400)
What it does? It draws a line from the coordinates (0, 120) to (100, 400). It has a nice design over it, the name is self explanatory and the elements are really descritive.
It is also great when you know it has no penality in speed whatsoever, the internal functions will be called and coordinated to make this happen while you simply talk with this interface.
FFI with C
The FFI interface with the C ABI is incredible. If you come from the background of C++ that you can use interchangeable or even D that you simply need to rewrite the header of the function to use it, you will find it pretty simple and easy. We have the native way of sbcl that is the package sb-alien, but also the cool way that is the cffi library that handles the heavy lifting nicely.
The way to make it work is to declare exactly like C, the types used and the signature of the functions, which can be time consuming, but rewards us with more control over the code. The cffi can define enums, structs, pointers and everything else without much trouble, like in this example:
// the C code:
int add(a int, b int) {
return a + b;
}
//===================
; the lisp version with cffi:
; the name %add is the name we give the function ourselves
(cffi:defcfun ("add" %add) :int
(a :int)
(b :int))
See? Really simple, we even have cffi:defcenum and other niceties.