Lisp Programming/Introduction

From Wikibooks, open books for an open world
Jump to navigation Jump to search

After Fortran, Lisp is the oldest programming language still in use today. It is a functional, imperative, dynamically strongly typed language, but markedly different from virtually every other language in existence. It can be compiled or interpreted, with good compilers producing code that runs almost as fast as C. The name "Lisp" is a contraction of List Processing, but, in the words of Paul Graham, Lisp is no more for processing lists than polo shirts are for polo.

Lisp has become known as a language for Artificial Intelligence systems, which was admittedly its original purpose. It has proven itself, however, to be a very versatile language, used in applications as diverse as web-applications, CAD software, and the Emacs text-editor.

Lisp code is very strange to look at at first, mainly because of its prefix notation. In infix notation, an expression is written with each binary operator between two operands:

2 + 3 + 4 + 5

In prefix notation, operators can take any number of operands, not just two. The operator comes first, followed by the operands:

(+ 2 3 4 5)

The parentheses are always required because they show where the arguments to the function start and end. This form of expression is known as an s-expression.

Function calls in Lisp also take the form of s-expressions: the first token in the parentheses specifies the function, and the remaining tokens and expressions in the parentheses are the arguments. For instance, a Lisp Hello, World program could be written:

(print "Hello, World")

(The function to print to standard output is called something other than print in some dialects of Lisp.)