Blog

Victor is a full stack software engineer who loves travelling and building things. Most recently created Ewolo, a cross-platform workout logger.

    Getting started with Lisp in 2019

    It's 2019 and Lisp stories are on fire at HackerNews. While there exist multiple Lisp dialects, Common Lisp is the oldest and most mentioned. Thus, if you're looking to get started with programming in Common Lisp, then there are 2 options:

    • Use portacle to get up and running with SBCL + Emacs.
    • Use roswell to install multiple Lisp implementations + build and bundle lisp applications. Roswell installs SBCL by default.

    As for me, I did not feel like learning a brand new text editor just to get started with Lisp so I went with Roswell. Follow the instructions on installing Roswell for your platform. In my case, I decided to install it under $HOME/bin on my linux machine via:

    $ sudo apt install rlwrap
    $ mkdir $HOME/bin
    $ git clone -b release https://github.com/roswell/roswell.git
    $ cd roswell/
    $ sh bootstrap
    $ ./configure --prefix=$HOME/bin
    $ make
    $ make install

    Make sure to add the roswell path to your ~/.bashrc (note that roswell installed itself under ~/bin/bin and if this looks odd to you, install it under $HOME/apps or something of the sort):

    if [ -d "$HOME/bin/bin" ] ; then
      PATH="$HOME/bin/bin:$PATH"
    fi

    Open a new terminal and setup roswell:

    $ which ros
    /home/xxx/bin/bin/ros
    $ ros --version
    roswell 19.06.10.100
    $ ros setup 

    We will now setup VSCode to run lisp. First install the vscode-lisp extension. Open a new file and type the following in:

    (defun main
      ()
      (format t "Hello world")
    )
    

    Then launch a terminal inside VSCode. In the terminal run rlwrap ros run to start the REPL (Read/Eval/Print Loop). Select the above defined function and use the "Run Selected Text in Active Terminal" from the Command Palette (F1) to run your code! Note that you can exit the REPL via: (SB-EXT:EXIT). The rlwrap utility remembers previously typed commands which makes for a much nicer REPL experience.

    What is amazing about Roswell is that is comes with a scripting / build ability that allows you to easily distribute your application. To see this in action first create a roswell script via ros init hello-world. Then add in the following code so that your script looks like the following:

    #!/bin/sh
    #|-*- mode:lisp -*-|#
    #|
    exec ros -Q -- $0 "$@"
    |#
    (progn ;;init forms
      (ros:ensure-asdf)
      ;;#+quicklisp(ql:quickload '() :silent t)
      )
    
    (defpackage :ros.script.hello-world.3774807541
      (:use :cl))
    (in-package :ros.script.hello-world.3774807541)
    
    (defun helloWorld
      ()
      (format t "Hello world")
    )
    
    (defun main (&rest argv)
      (declare (ignorable argv))
      (helloWorld))
    ;;; vim: set ft=lisp lisp:
    

    We can now simply run this script via ros hello-world.ros but more interestingly, we can actually compile a binary via ros build hello-world.ros && ./hello-world.

    Interestingly enough, I am not a complete noob to Common Lisp, I actually programmed it 15 years ago during my undergrad years. A colleague and I compared an experimental genetic algorithm against an ant colony optimization algoritm on a path-finding problem. Once I had lisp running I opened up the project and basically executed ants.lisp in the REPL, ran (INITIALIZE-ANT-WORLD) followed by (DISPATCH-ANTS) and voila, my ants were able to find their food!

    No guide to getting started with Lisp would be complete without a list of further reading that will keep you busy for the next 100 years so here we go:

    • A very basic Lisp tutorial which also features an excellent list of Lisp books
    • A road to common lisp - Steve Losh's excellent guide to getting into Lisp programming. The following is a small snippet of useful information from the post:
      • Files are files on your hard drive.
      • Packages are containers of symbols. They are orthogonal to files.
      • Systems are collections of code, instructions on how to load that code, dependency lists, and metadata. They are orthogonal to packages.
      • Projects are high-level collections of "stuff" such as code, documentation, maybe some image assets, etc. They are (mostly) orthogonal to systems.
      • Common Lisp itself knows about files and packages.
      • ASDF adds systems.
      • Quicklisp adds the internet.
      This guide also provides a very nice review of libraries and is definitey worth a read.
    • Another article that recommends roswell and provides instructions for Atom integration along with project and library management.

    Well, that's about it - Lisp is beautiful and I'm off to wrap my head around some 15 year old code that doesn't look too bad, go functional programming!

    HackerNews submission / discussion

    Back to the article list.

    SmallData newsletter

    Subscribe to get articles as they are published direct to your inbox!