Lisp (programming language)
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)[change | change source]
(/ (+ 3 5) 4) ; returns 2
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 or not. If , then (factorial 0) is 1. If , then (factorial n) returns the product of and the factorial of .
(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
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,
(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
The operation cdr returns everything in the list except for the first thing.
(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
Here is a hello world program in Scheme:
(display "Hello world!")