LISP

From GPWiki

(Redirected from Lisp)

Files:GUITutorial_warn.gif The Game Programming Wiki has moved! Files:GUITutorial_warn.gif

The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server.

However, the GPWiki forums are still active! Come say hello.

Lisp is a peculiar language characterized by the following things:

  • Lisp programs consist of Lisp data. In other words, all the elements used in Lisp code also have a runtime representation - most of it is lists (singly linked lists), strings, numbers and symbols.
  • The amount of syntactic sugar in Lisp languages is small - most things are expressed by parenthesed lists. Thus parentheses are abound in Lisp code. Whether this makes it very clear or completely unreadable depends on who you are asking.
  • Because code is data, code-transforming macros are very powerful, and are used a lot to make code more abstract.
  • All modern Lisps have garbage collection, first-class functions, and other conveniences found in modern high-level languages.
  • Lisp users have a tendency to be unsufferably smug about the power of the language on the fact that a lot of useful programming idioms were first invented in Lisp.
  • Partly because of this smugness, many non-Lisp programmers have a profound dislike of everything related to Lisp.

Here's a taste of Lisp code:

(defun filter (fn lst)
  (if (null lst)
      ()
      (let ((result (funcall fn (car lst))))
        (if result
            (cons result (filter fn (cdr lst)))
            (filter fn (cdr lst))))))

This is Common Lisp code defining a function of two arguments. It recursively applies its first argument - a function - to its second argument - a list - and returns a list of the resulting elements, leaving out those where the function returned nil (false). (This is not the most efficient implementation of such a function, as usual more efficiency can be gained by making the function less straightforward.)

Before this code can be run, the Lisp environment reads it (transforms it from text to a data structure). This function only contains (nested) lists and symbols. After reading, macros are applied. In this case defun is a macro that transforms to more primitive function-defining code for example. The resulting code is compiled (almost all recent Lisp environments compile rather than interpret code) and executed - resulting in the storing of this particular function under the symbol filter. Function calls are lists starting with the name of the function, in the example null and cons are functions, and the function defined here can be called like (filter #'1+ '(1 2 3)), which will return (2 3 4).

There have been a lot of different Lisp languages, but at the moment only a few of them are really relevant anymore:

  • Common Lisp is the result of taking all the neat stuff from a series of different Lisp dialects and trying to standardize it into a single language. It is a very big and powerful language, but has some ugly warts and obsolete pieces.
  • Scheme goes the other way, it is a very minimal and elegant Lisp. Its beauty is stunning, but in practice it lacks features (specific implementations often add non-standard extensions to fix the most glaring deficiencies).
  • Emacs Lisp is the primary extension language for Emacs and XEmacs. It has much in common with Common Lisp but lacks CLOS and lexical scoping (no closures).
  • AutoLisp was the original extension and macro language for AutoCAD. AutoLisp is by far the least powerful or common of the Lisps in continuing use, but there are curiosities like a clone of the Lunar Lander game written in it.

Scheme and Common Lisp are well-standardized. Scheme is still evolving (The R6RS version of the specification is around the corner), while the Common Lisp spec has been stable for a decade. Both also have moderately-sized, very enthusiastic communities around them. Free implementations and libraries are abundant, but some of these (mostly libraries) require you to be a dedicated Lisp hacker to be able to understand and use them.

There are minor Emacs-like editors extended through other Lisp variants (Edwin for Scheme and Hemlock with CMUCL), though none of them rivals Emacs or XEmacs in popularity.

External links