📜 ⬆️ ⬇️

Developing web applications in Common Lisp (part one)

This review is a small guide for those who decide (or decide) to entrust the future of their startup to this wonderful language. Despite the fact that the main focus will be on web development, I will try to cover also more general topics related to Common Lisp in one way or another. The material will be gathered from my own experience in developing the AlterMoby web service.

The first part of this review will be introductory. Experienced lispery can safely skip it. In this part, I will try to explain when to use Lisp, and which implementation is best suited for building web applications. The latter, of course, will reflect only my subjective point of view.


When to use Lisp

Many modern web-oriented languages ​​have in their arsenal much of what once belonged exclusively to the Lisp language (such things primarily include anonymous functions and functionals). Therefore, using Lisp to create typical web applications, you most likely will not feel great advantages compared to the same PHP, Python or Ruby. Quite the contrary, since the Lisp community is quite small and it will be harder for you to find a ready-made solution. However, Lisp has one key feature that can raise an abstraction to a level unattainable for any other language. I'm talking about the possibilities of the program to build and run another program during its execution (or compilation), i.e., about metaprogramming.
Let me give you a toy example in Common Lisp that demonstrates this feature:


This simple code defines a function that constructs another function with a given number of arguments. The latter calculates their sum. Of course, this is a contrived example (operator + calculates the sum of an arbitrary number of arguments), but it demonstrates a unique Lisp property that distinguishes it from other languages. Metaprogramming is also implemented by macros (please do not confuse with C / C ++ macros). Macros are the same generating functions, but used not during the execution of the program, but during its compilation.

When will Lisp be useful in your project? First, when the implemented application must have rather complex semantics, in other words, if high flexibility of the program code is required. Secondly, when the system is built by a small number of programmers, and it is inappropriate to neglect automation at all levels of development (in such cases the inconvenience of the low prevalence of this language will be outweighed by the potential code savings). In addition, Lisp is recommended to use for the implementation of web-applications with a large load (oddly at first glance). Many modern Lisp compilers generate machine code that is an order of magnitude faster than the same PHP or Python (I’m not talking about Ruby at all).

Dialect and implementation

As you know, there are two main Lisp dialects: Common Lisp and Scheme . I will not talk here about the differences between them (can be found in abundance on the pages of the Internet). In my subjective opinion, it is better to use Common Lisp for writing web applications for the following reason. The Scheme standard describes the syntax and semantics of the language itself, ignoring standard libraries. As a result, we have an elegant, but toy language with numerous incompatible extensions (so necessary for application programming). In turn, Common Lisp, despite its many shortcomings, clearly regulates most of the things that will be needed in a real project. These include, in particular, the external call protocol, which allows you to communicate with libraries written in C. Therefore, Common Lisp is a real industrial language with a large number of open libraries implementing this or that functionality.

Now about the implementation. There are quite a few qualitative implementations of Common Lisp. They can be divided into proprietary and free. By proprietary include LispWorks and Allegro CL . Both systems have broad capabilities, but they cost a lot (especially the second). If you don’t feel sorry for the money, you can get a great product with quality support. But before making such a decision, pay attention to the camp of free implementations. It contains very worthy representatives.

Qualitative free implementations include CLISP , SMUCL and SBCL . The first is a cross-platform bytecode compiler. Hence the need for a runtime environment and low productivity. SMUCL is a high-quality native compiler for Unix systems, generating very efficient machine code. Its lack of incomplete support for the standard and the presence of many conflicting atavisms (after all, the project has a long and rich history). SBCL is a successful attempt to reform SMUCL. Preserving the general line of succession, SBCL is more logical, being cleared of many historical growths and layers of SMUCL. The conditional flaws of both these compilers can be attributed to weak portability, limiting their work only on Unix-systems. SBCL has experimental support for Windows, but it is not complete (in particular, there is no multithreading), so serious projects on this platform can still be forgotten.

In conclusion of the first part of my review, I would recommend to stop on the SBCL compiler. It is a free, high-quality and constantly developing product that you can rely on in the most serious projects.

Source: https://habr.com/ru/post/104349/

All Articles