Lisp (programming language)

(Redirected from LISP)

Lisp (used to be called LISP) is a programming language. It is among the oldest programming languages that are still used today. Only Fortran is one year older. Lisp was designed by John McCarthy in 1958. The best-known versions of LISP are Common Lisp, Scheme and Clojure. Many concepts that are used in modern programming languages were first created in Lisp. Linked lists are a very important data structure in Lisp. The basic concepts behind Lisp are easy to learn. Logo is another version of Lisp that was made for children. Logo can help young children develop skills and become efficient within the programming language.

Simple examples (Scheme)

In Lisp, operations are written in prefix notation, and they start and end with parentheses. For example, the formula [math]\displaystyle{ \frac{3+5}{4} }[/math] is written as:

<syntaxhighlight lang="scheme"> (/ (+ 3 5) 4)

returns 2

</syntaxhighlight>

Because Lisp is a functional language, Lisp programs often use recursion to solve problems. Here is a Scheme program that finds the factorial of a number. The function (factorial n) starts by testing if [math]\displaystyle{ n=0 }[/math] or not. If [math]\displaystyle{ n = 0 }[/math], then (factorial 0) is 1. If [math]\displaystyle{ n\neq0 }[/math], then (factorial n) returns the product of [math]\displaystyle{ n }[/math] and the factorial of [math]\displaystyle{ n-1 }[/math].

<syntaxhighlight lang="scheme"> (define (factorial n)

   (if
     (= n 0)
     1
     (* n (factorial (- n 1)))))

(factorial 6)

returns the factorial of 6
6! = 6*5*4*3*2*1 = 720

</syntaxhighlight>

Linked lists are an important idea in Lisp. The list without any things in it is known as the empty or nil list, and is written as '(). A list that has things in it is written as '(dog cat).

The operation car is used to get the first thing of a list. For example,

<syntaxhighlight lang="scheme"> (car '(dog cat fish))

returns 'dog, which is the first thing in the list

(car '(1 2 3 4 5 6 7))

returns 1

(car '())

this code gives an error because the list is empty/nil

</syntaxhighlight>

The operation cdr returns everything in the list except for the first thing. <syntaxhighlight lang="scheme"> (cdr '(dog cat fish))

returns '(cat fish)

(cdr '(6))

returns '(), because there is nothing in the list after the first thing (6)

(cdr '())

gives an error because the list is empty/nil

</syntaxhighlight>

Here is a hello world program in Scheme:

<syntaxhighlight lang="scheme"> (display "Hello world!") </syntaxhighlight>