Category Archives: Lisp

NB: Control SBCL optimizations

Note, that you can control how SBCL optimizes certain function (or performs global optimizations).

You can use declare block in functions:

(declare (optimize (safety 3) (debug 3) (speed 0) (space 0)))

This directive means: favor safety and debug over speed and space. The more value is associated with the corresponding directive (i.e. safety, speed, debug or space) the more SBCL compiler values this parameter when it compiles your source.

Debugging priority might be established globally by similar declaration:

(declaim (optimize (safety 3) (debug 3) (speed 0) (space 0)))

SBCL also has internal function that restricts compiler to assign the corresponding weight to the certain compilation parameter:

(sb-ext:restrict-compiler-policy 'debug 3)

How to install and use Common Lisp on Linux/Windows/Mac OS X

You can easily find related information on the web. One of the easiest solutions is to use lisp-in-the-box distribution, but it is a bit outdated. In order to use recent versions you’d keep your lisp compiler, slime and asdf package up-to-date.

This is what we need:

  • Common Lisp compiler. I use CLISP and sometimes SBCL. You may choose whatever you like but it shall be compliant with Common Lisp ANSI standard.
  • Emacs. If you aren’t familiar with this greatest editor of all times, you’d better try it. You will be greatly paid off.
  • Slime – roughly speaking you can think of it as of kind of a “plug-in” for emacs that makes emacs aware of how to evaluate lisp files and interact with REPL.
  • ASDF (this is optional package, but you will need it because almost all the modern lisp libraries packaged with it). Basically you need just one file asdf.lisp.

Let’s get started:

  1. Install lisp compiler. You need to remember location to it’s installation directory, e.g. “C:\Programs\clisp-2.47” or “/opt/sbcl-1.0.22” cause it will need later.
  2. Install emacs (by unpacking it’s binary distribution on windows or executing “sudo apt-get emacs” on Ubuntu).
  3. Install slime (just unpack it’s distribution somewhere).
  4. Install asdf in the same way you installed slime.
  5. Make your lisp pre-load asdf.lisp before it starts REPL. Paste the following line to your ~/.clisprc file (~/.sbclrc for SBCL):
    (load “e:/bin/asdf/asdf.lisp”)

    (load “e:/path/to/your/asdf/installation/asdf.lisp”). Change the value in quotes to full path to your asdf file (see step 4).

  6. You will need to tune your emacs to make it know where your lisp compiler and slime resides. The file the emacs settings resides in is located in your home directory, you can refer it using the following way in emacs editor, no matter what OS you use: ~/.emacs. If you don’t know how to edit emacs init file you may use my excerpt of my .emacs file (significant part typed in red and made bold):
    ;; .emacs file
    ;; =============================================================================
    ;; slime: clisp interaction
    
    (add-to-list 'load-path "/path/to/your/slime/dir/slime-2009-10-12")
    (setq inferior-lisp-program "/path/to/your/clisp/executable/bin/clisp")
    (require 'slime)
    (slime-setup '(slime-scratch slime-editing-commands slime-repl))
    
    
    ;; =============================================================================
    ;; global key bindings
    
    ;; Meta
    (global-set-key "\M-\C-h" 'backward-kill-word)
    (global-set-key "\M-g" 'goto-line)
    (global-set-key "\M-r" 'replace-string)
    
    ;; Function keys
    (global-set-key [f1] 'manual-entry)
    (global-set-key [f2] 'info)
    (global-set-key [f3] 'grep)
    (global-set-key [f5] 'slime)
    (global-set-key [f7] 'other-window)
    (global-set-key [f8] 'dired)
    
    ;; =============================================================================
    ;; mixed indentation
    (setq indent-tabs-mode nil)
    
    ;; =============================================================================
    ;; C/C++ mode
    
    (defun apply-settings-for-c-and-c++ ()
      (c-set-style "bsd")
      (define-key c++-mode-map [return] 'newline-and-indent)
      (setq c-basic-offset 4))
    
    ;; indentation style for C++-files
    (add-hook 'c++-mode-hook
     '(lambda ()
        (apply-settings-for-c-and-c++)
        ;; custom c++ settings
        (c-set-offset 'innamespace 0) ; no indent after namespace
        (c-set-offset 'inline-open 0) ; no indent after inline functions defined in classes
        ))
    
    (add-hook 'c-mode-hook
     '(lambda ()
        (apply-settings-for-c-and-c++)))
    
    ;; =============================================================================
    ;; lisp mode
    
    ;; indenting by pressing just [enter] - not C-j
    (defun set-local-indent ()
      (local-set-key [return] 'newline-and-indent))
    
    ;; selects uplevel s-expression
    (defun select-uplev-sexp ()
      "Selects upper-level s-expression"
      (interactive)
      (backward-up-list)
      (mark-sexp))
    
    ;; this applies generic style for all lisps
    (defun generic-lisp-mode-setups ()
      (show-paren-mode 1) ; emacs-only, show matching parens
      ;;(paren-set-mode 'sexp) ; xemacs extension, full s-expression shall be highlighted (other option is 'paren)
      (define-key lisp-mode-map [return] 'newline-and-indent) ; works for .xemacs
      (set-local-indent)
    
      ;; new key bindings for lisp:
      (local-set-key [f1] 'select-uplev-sexp)           ; goes to uplev s-exp and selects it
      (local-set-key "\C-b" 'backward-sexp)             ; moves backward by s-exp
      (local-set-key "\C-f" 'forward-sexp)              ; moves forward by s-exp
      (local-set-key (kbd "C-M-<backspace>") 'backward-kill-sexp) ; kill prev s-exp
      (local-set-key "\C-p" 'backward-kill-sexp)
      )
    
    (add-hook 'lisp-mode-hook
     '(lambda ()
        (generic-lisp-mode-setups)
        ))
    
    ;; emacs lisp mode
    (add-hook 'emacs-lisp-mode-hook
     '(lambda ()
        (generic-lisp-mode-setups)))
    
    ;; =============================================================================
    ;; switch off backup files (not sure if it would be useful for you)
    (setq make-backup-files nil)
    ;; end of file
  7. That’s it! Now launch emacs, press [F5] (if you use my .emacs file, alternatively try ‘M-x slime’) and start using REPL!