Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
93 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

Introduction

This article is intended to be a quick overview of the Lisp family of languages, providing a glimpse into what makes the language different from other programming languages.

First of all, many people think that Lisp is for AI and only for AI.  Questions of the form "How do you do AI with Lisp?" commonly pop on on discussion forums.  Although historically the main use of Lisp in decades past was for AI research, you can do AI in other languages, and you can do other things (such as graphics, UI, scripting, databases) in Lisp.

Lisp is a computer language, much like other computer languages.  Most of the language overlaps in functionality with "modern" languages such as C, C++, Python, and Java.  What makes Lisp interesting are those features which set it apart from the other languages, and from which much of the attraction for (and disdain of) the language stem.

The Lisp Family

There are currently two main dialects of the Lisp language in use.

The first is Common Lisp, which is an industrial strength ANSI standard language with many powerful built-in features such as exceptions and object oriented facilities.

The second is Scheme, which is a small, very elegant, minimal language which in some ways is even more powerful than Common Lisp.

For simplicity's sake, we will use Scheme for our discussion and examples, though almost all we say will be equally applicable to Common Lisp.

Lisp And C

Much of the programming in Lisp involves the same sort of things one does in other languages:

C Scheme
float double(float x)
{
    return x * 2;
}
(define (double x)
  (* x 2))
float func (float x)
{
    if (x > 0) {
        return double(x);
    } else {
        return -x;
    }
}
(define (func x)
  (if (> x 0)
    (double x)
    (- x)))
float pi = 3.1415926; (define pi 3.1415926)
Example 1.

Anyone who knows how to program in C should be able to look at the Scheme examples above and understand what they do.

Let's look a bit closer at how the Scheme code differs from C:

  • Notice how there is no difference between the definition of a function and that of a global variable.  Both use define.  In fact, in Lisp there is no distinction between a statement and a procedure.
  • There are no type declarations in the Scheme code.  Lisp is a dynamic language, which loosely put means that what is important is not what something is, but rather what it can do.  If you pass a string in for x, you will get a runtime error, but an integer (or rational or complex number, both of which are standard Scheme types) will work fine.
  • The functions have no return statements.  All expressions in Lisp have a value.  The value of a function is simply the value of the last evaluated expression.  C programmers are able to use this feature in limited cases, as the code fragment below shows:

C Scheme
x +(y > 0 ? y : 2*y); (+ x (if (> y 0) y (* 2 y)))
Example 2.

  • All the Lisp expressions are in prefix format.  x + 2 in C becomes (+ x 2) in Lisp.
  • The code in Lisp consists of expressions, not a mixture of expressions and statements as in C.  Even function definition and conditionals are expressions.
  • Every expression is enclosed in parentheses.  We will see later why this is a good thing.

So how is Lisp different from C?  What does it do that C does not? And what's up with all those parentheses anyway?

There are a number of ways in which Lisp differs from C:

  • It is a higher level language.  By this, I mean that there is more between the language and the hardware than in C.  You cannot access memory locations, cannot do pointer math, etc.  Whether this is a good thing or a bad thing depends on what you are doing, and the type of programmer you are.  In a nutshell, it means you can worry less about the hardware and concentrate on more abstract issues.
  • It handles memory allocation and garbage collection for you.  There are no memory bugs or dangling pointers in Lisp.
  • Lisp has first class functions and closures.  This means that a function can create and return another function on the fly.
  • Lisp has macros which go way beyond the string substitution macros of C.  Macros in Lisp allow you to extend the language, as well as write whole new languages on top of Lisp.

The last two points merit more discussion, as they are what sets Lisp apart from most other languages.  People who have never used these features do not realize their power, and therefore do not miss them when using other languages.  Lisp programmers often immediately notice and rue the lack of these features when programming in other languages.  The language you use shapes the way you think.



Page 2

Contents
  Page 1
  Page 2

  Printable version
  Discuss this article