Lisp Programming Tutorial

This Lisp Programming tutorial post explains Lisp programming quickly that helps you in quick revision. LISP programming tutorial contains all basic concepts of the LISP programming language. It is also explaining syntax operators of LISP programming. I will also update this Lisp programming tutorial page on regular basis.

Lisp Introduction​

The name LISP derives from “LISt Processor”. Lisp is a family of programming languages with a long history. It is the second-oldest high-level programming language after the Fortran which is only one year older than Lisp. Today, the best-known general-purpose Lisp dialects are Racket, Common Lisp, Scheme, and Clojure.

Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT). I was originally created as a practical mathematical notation for computer programs, influenced by the notation of Alonzo Church’s lambda calculus. Linked lists are one of Lisp’s major data structures, and the Lisp source code is made of lists.

 

Features of Common LISP

  • It provides an object-oriented condition system.
  • It allows updating the programs dynamically.
  • It provides a convenient macro system.
  • It is machine-independent
  • It uses iterative design methodology and easy extensibility.
  • It provides high level debugging.
  • It provides advanced object-oriented programming.
  • It provides wide-ranging data types like, objects, structures, lists, vectors, adjustable arrays, hash-tables, and symbols.
  • It is expression-based.
  • It provides a complete I/O library.
  • It provides extensive control structures.

LISP - Program Structure

Lisp is an expression-oriented language. LISP expressions are called symbolic expressions or s-expressions. These s-expressions are composed of three valid objects, atoms, lists, and strings. In lisp, all code and data are written as expressions and any s-expression is a valid program. LISP programs run either on an interpreter or as a compiled code. The interpreter checks the source code in a repeated loop, which is also called the read-evaluate-print loop (REPL). It reads the program code, evaluates it, and prints the values returned by the program.

An Example Program

Let us write an s-expression to find the sum of three digits 24, 6, and 27. To do this, we can type at the interpreter prompt.

(+ 26 6 27)

LISP returns the result:-

59

If you would like to run the same program as a compiled code, then create a LISP source code file named test.lisp and type the following code in it.

(write (+ 26 6 27))

When you click the Execute button or type Ctrl+E, LISP executes it immediately and the result returned is:−

59

Prefix Notation in Lisp

In the above example program, you can see that prefix notation is used by the LISP. In the above program, the + symbol works as the function name for the process of summation of the numbers.

In prefix notation, operators are written before their operands. For example, the expression,

(/ (* a (+ b c) ) d)

This will be also written as:−

(/ (* a (+ b c) ) d)

Let us see another example, where I am writing a code for converting Fahrenheit temp of 60o F to the centigrade scale.

The mathematical expression for this conversion will be,

(60 * 9 / 5) + 32

Create a source code file named test.lisp and type the following code in it.

(write(+ (* (/ 9 5) 60) 32))

When you click the Execute button or type Ctrl+E, LISP executes it immediately and the result returned is:−

140

Evaluation of LISP Programs

Lisp languages are often used with an interactive command line, which may be combined with an integrated development environment (IDE).

The user types expressions at the command line or directs the IDE to transmit them to the Lisp system. Lisp reads the entered expressions, evaluates them, and prints the result. For this reason, the Lisp command line is called a read–eval–print loop (REPL).

The evaluation process takes the following steps:-

  • The reader translates the strings of characters to LISP objects or s-expressions.
  • The evaluator defines the syntax of Lisp forms that are built from s-expressions. This second level of evaluation defines a syntax that determines which s-expressions are LISP forms.
  • The evaluator works as a function that takes a valid LISP form as an argument and returns a value. This is the reason why we put the LISP expression in parenthesis because we are sending the entire expression/form to the evaluator as arguments.

If you are looking for an online video course on Lisp programming tutorial, then you can check this course that created by my friend Joshua Modglin. Good thing is that 1 month Free Trial available for this course including thousands of courses.

Basic "Hello World" program using LISP

Generally, we start any programming language with a “hello world” because it helps us to understand the basic structure of the programming language.

So here we will also see a lisp program that will print hello world on the console. But before writing the code you need to a project file where you will write the code.

So, please create a new source code file named main.lisp and type the following code in it.

 

(write-line "Hello World")

(write-line "HI Aticleworld, I am Learning LISP")

 

When you will execute the code output will be:

Hello World

HI Aticleworld, I am Learning LISP

 

 

Basic Syntax of LISP

LISP programs are made up of three basic building blocks and these blocks are Atom, list, and string.

Atom:

In one sense atoms are likely variables in other languages. An atom is a number or a symbol. An atom is a number or string of contiguous characters. It includes numbers and special characters.

Some of the valid examples of the atom are

hello-from-Aticleworld
name
123008907
*hello*
Block#221
abc123

 

List:

A list is a sequence of atoms and/or other lists enclosed in parentheses.

Some of the valid examples of the list are

( i am a list)
(a ( a b c) d e fgh)
(father tom ( susan bill joe))
(sun mon tue wed thur fri sat)
( )

 

string

A string is a group of characters enclosed in double quotation marks.

Some of the valid examples of the string are:

" I am a string"
"a ba c d efg #$%^&!"
"Please enter the following details :"
"Hello from 'Aticleworld.com'! "

How to add a comment in LISP

Using the semicolon we can add a comment in LISP programming. Let’s see an example where I am putting comments in the LISP code.
 

(write-line "Hi There") ;
;I am comment
(write-line "I Love to read Aticleworld.com! Learning LISP programming tutorial")

When you run this program then out will be,

Hi There
I Love to read Aticleworld.com! Learning LISP programming tutorial

Some important point you should remember

  • The basic numeric operations in LISP are +, -, *, and /

  • LISP represents a function call f(x) as (f x), for example cos(45) is written as cos 45

  • LISP expressions are case-insensitive, cos 45 or COS 45 are same.

  • LISP tries to evaluate everything, including the arguments of a function. Only three types of elements are constants and always return their own value

    • Numbers

    • The letter t, that stands for logical true.

    • The value nil, that stands for logical false, as well as an empty list.

Naming Conventions in LISP

  • Name or symbols can consist of any number of alphanumeric characters other than whitespace, open and closing parentheses, double and single quotes, backslash, comma, colon, semicolon, and vertical bar are contained in name or symbols.
  • These characters can be used in a name, to use escape character (\).
  • A name can have digits but not entirely made of digits, because then it would be read as a number and also name can have periods, but can’t be made entirely of periods.

Use of Single Quotation Mark

  • The function arguments and list members are evaluated by LISP.
  • The atoms or lists are taken literally at times and don’t want them evaluated or treated as function calls.
  • For this, we need to precede the atom or the list with a single quotation mark.

Let’s see an example to understand,

(write-line "single quote used, it inhibits evaluation")
(write '(* 2 3))
(write-line " ")
(write-line "single quote not used, so expression evaluated")
(write (* 2 3))

Output:

single quote used, it inhibits evaluation
(* 2 3) 
single quote not used, so expression evaluated
6

Some Recommended Post