How to start writing Lisp code?

Often you have to see newbies try Common Lisp and then complain that it’s impossible to work with it normally. As a rule, this is due to the fact that they do not understand how to set up a process for themselves that provides that same “quick response” from the development environment, when you changed the function, compiled it and the changes immediately began to be used inside the already “running” program without restart it.


To understand how this looks, you can watch some video on youtube, which demonstrates interactive development on Common Lisp.



Today I want to show how to set up a dev environment for such development. In 2018, it became quite simple, thanks to the constantly improving tuling.


I apologize in advance for the fact that the following videos are recorded in Asciinema, and Habrahabr does not support it. Click on the screenshots of the console to watch videos.


First you need to install SBCL , Roswell and Emacs . I'll tell you on the example of installing everything in OSX, and I will be glad if in the comments you share your experience on Windows and Linux. Then I can supplement the article with examples for all three platforms.


SBCL is one of the many implementations of Common Lisp. From open source - the fastest. If desired, the SBCL can run code at a speed comparable to the code in C ++, but at the same time having all the buns from fast interactive development.


Roswell, is a utility for installing and running Common Lisp programs. Including she knows how to run pre-configured Emacs, as well as build programs into binaries.


You probably know Emacs — an operating system with a code editor. You can write in Common Lisp in any other editor, but today Emacs has better integration and support for code editing. With him you do not have to count brackets, he does everything for you.


So, if you are using OSX, then you need to do


brew install roswell emacs 

After the brew has run a disk and put everything in place, just run it in the terminal:


 ros run 

This command will automatically install the latest version of SBCL and start the Lisp repl, where you can enter the code:



But this is not the case, to be developed so nelya. So let's set up Emacs for full development:


 ros emacs 

The command will launch Emacs in the console and configure Quicklisp, a package manager for Common Lisp.
But before we continue, let's set up a terminal, emacs, and OSX so that they work well together.


First you need to change some settings in OSX and iTerm


Make CapsLock work like a Control. In Emacs, without this, nowhere:



Then disable all combinations associated with the use of Control and arrows in MissionControl:



Then put iTerm2 and switch Alt behavior from Normal to Esc + in the profile settings:



After that, create a file with a minimum config for Emacs, ~/.emacs.d/init.el :


 (package-initialize) (require 'package) (add-to-list 'package-archives '("MELPA" . "http://melpa.milkbox.net/packages/") t) (defun install-package (package) (unless (package-installed-p package) (package-refresh-contents) (package-install package))) (install-package 'paredit) (install-package 'expand-region) (defun setup-lisp-mode () (require 'paredit) (paredit-mode) (define-key paredit-mode-map (kbd "Cw") 'paredit-backward-kill-word)) (add-hook 'lisp-mode-hook 'setup-lisp-mode) (add-hook 'emacs-lisp-mode-hook 'setup-lisp-mode) ;;  Cw           ;;  kill-region (global-set-key (kbd "Cw") 'backward-kill-word) ;;      kmacro- (global-set-key (kbd "Cx Ck") 'kill-region) ;;  indent-new-comment-line (global-set-key (kbd "Mj") (lambda () (interactive) (join-line -1))) ;;    (global-set-key (kbd "Cc rs") 'replace-string) (global-set-key (kbd "Cc rr") 'replace-regexp) ;;    emacs    ;;      =,   ;;    . (global-set-key (kbd "Cc =") 'er/expand-region) ;;      , ;;        (global-set-key (kbd "Cc c") 'comment-or-uncomment-region) (global-set-key (kbd "Cc C-\\") 'goto-last-change) (setq custom-file "~/.emacs.d/customizations.el") (when (file-exists-p custom-file) (load custom-file)) 

After that, run ros emacs again, press Alt-X and enter the slime command. As a result, we get the command line for entering Lisp commands:



Now you can already code


But we will not enter commands into the repl, it’s better to start developing microservice right away. If you only need an API, then the easiest way to use Ningle . If you need a more advanced framework such as jung, then you can try Radiance or Caveman2 . But now we will not do anything complicated, but muddle a simple HTTP apish.


Open the server.lisp file ( Cx Cf server.lisp ) in Emacs and start writing code. Like that:



As a result, a web server will be running inside your instance, to which you can add routes and redefine views on the fly.


Here is all the code for the lazy:


 ;; Micro-framework for building API (ql:quickload :ningle) ;; Now ningle is installed and we need to install a clack which is analog of WSGI ;; stack from Python ;; I've pressed Cc Cc to eval this form (ql:quickload :clack) ;; To parse json: (ql:quickload :jonathan) (defvar *app* (make-instance 'ningle:<app>)) (setf (ningle:route *app* "/") ;; in case, if you need to parse or serialize JSON, ;; use Jonthan library. (jonathan:to-json '(:foo 100500))) (defvar *server* nil "This variable will store currently running server instance.") (defun start () (if *server* (format t "Server already started") (setf *server* (clack:clackup *app*)))) (defun stop () (if *server* (clack:stop *server*) (format t "Server is not running"))) 

In Lisp, constructs that are inside parentheses are called “forms.” Forms that are not nested in the top are called top-level. Such forms can be compiled by pressing the Cc Cc combination when the cursor is inside such a form. If you switch CapsLock to ontrol , then this combination is very convenient to click.


After the form has been compiled, the new version of the function will immediately take effect and no server restart is required. So it is very convenient to debug and fix bugs. In addition, you can configure the automatic test run immediately after compiling a part of the code, but this is a completely different story.


If you are interested in any other topics, write in the comments, I will try to make posts about them.

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


All Articles