Skip to main content

WHAT is LISP ?

LISP was invented by John McCarthy in 1958. It is high level programming language. It was first implement by Steve Russell on an IBM 704 Computer.

Features of LISP

1. It is based on expressions. the prefix notations are used to represent the expression.
2. It has advanced object oriented features.
3. It has got automatic garbage collection. Hence the programmer need not have to explicitly free the dynamically allocated memory.
4. A strong support for recursion in programs. Due to this feature this language is widely used and accepted in artificial intelligence.
5. All computation is performed by applying functions to arguments. Variable declarations are rarely used.

Applications Areas

LISP is mainly used in:-

1.Artificial intelligence robotics.
2.Computer games.
3.Pattern Recognition.
4.Real time embedded systems
5.list handling and processing 
6.for creating applications for educational purpose.
7.For List handling and processing.

::::::::::::::::::::::::::::Now get Started for coding in LISP:::::::::::::::::::::::::::::::


For write code we use GNU CLISP 2.49.(you can download it from here GNU LISP)

About GNU CLISP 

  • ANSI Common Lisp is a high-level, general-purpose programming language. GNU CLISP is a Common Lisp implementation by Bruno Haible of Karlsruhe University and Michael Stoll of Munich University, both in Germany.
  •  It mostly supports the Lisp described in the ANSI Common Lisp standard. It runs on most GNU and Unix systems (GNU/Linux, GNU/Hurd, FreeBSD, NetBSD, OpenBSD, Solaris, Tru64, HP-UX, BeOS, IRIX, AIX, Mac OS X and others) and on other systems and needs only 4 MB of RAM.
  •  The user interface comes in English, German, French, Spanish, Dutch, Russian and Danish, and can be changed during run time. GNU CLISP includes an interpreter, a compiler, a debugger, CLOS, MOP, a foreign language interface, a socket interface, i18n, fast bignums, arbitrary precision floats and more.
  •  An X11 interface is available through CLX, Garnet, CLUE/CLIO. GNU CLISP runs Maxima, ACL2 and many other Common Lisp packages.

After Installation of  CLISP following window and > prompt will appear.


In LISP, nearly everything is function . For example
( + ( * 10 20 ) 30 ) 
will result in 230.

Before going to learn how to write this functions we first see the programming style of LISP


Programming style in LISP

1.All LISP entries must be written in bracket.

2.white space are essential in an LISP instruction.

3.There must be the same number of openning bracket as closing bracket.

4. setq is used to assign value to variable .

5.( setq x 10 ) assign value 10 to variable x.

6. ! is used to print contents of a variable.

7. !x will print value 10.

8. The line ( setq b ( + 8 3 ) ) means add the number 3 into 8 and assign the result of the operation to b.

9. Inner bracket '(' are performed first then outer bracket ')'.   

Now,lets learn the LISP step by step through simple Programms


1.Write a Program to perform Airthmetic operations.

>>( + 3 2 )   

Output>>  5  

this can be explain as , according to rule 1 of programming style "All LISP entries must be written in bracket."so "+ 3 2" is written in bracket. In lisp,operator are implemented using preorder i.e first operator(i.e. + - * / > < >= <=),followed by pair of operand. 

Now in above expression you can use any airthmetic operator(-,*,/).

2.Write a program to execute following statement using if condition.

>> ( if( <= 3 2 ) ( + 4 3 ) ( + 3 2 ) )

Output>> 5

this can be explain as, 'if'' execute only one statement from two  based on the Condition.
the expression written after if {i.e ( <= 3 2 ) } is condition. the expression ( + 4 3 ) is the first statement and ( + 3 2 ) is the second statement . 
the logic of ' if'' is that if condition is true the first statement will execute else second statement  will execute.
In the above example the condition is 3 <= 2  i.e. false so second statement will execute i.e. ( + 3 2 )
and result in 5.
* if condition is apply between two statement only  with a condition , not apply with more than two statement ,if we want this we use another technique. 

Practice yourself,

>> ( if ( >= 5 9 ) ( + 5 9 ) ( - 5 9 ) )

Output>> -4

From above two Problem,we understand how we apply arithmetic operator and if condition in LISP. 

Now, we learn how to write function in LISP

The macro named defun is used for defining functions. The defun macro needs three arguments:
  • Name of the function
  • Parameters of the function
  • Body of the function
Syntax for defun is:

(defun functionname (parameter-list) "Optional documentation string." body) 

For example: Define a LISP function to compute sum of two Numbers.

write the below code in GNU CLISP 2.49
>> ( defun sum ( x y ) ( + x y ) )
>> to see the output write below code in LISP
>> ( sum 3 2 )
output>> 5













3.Define  a LISP function to compute difference of square if x>y return x*x -y*y otherwise y*y-x*x.

>>( defun square_diff ( x y ) ( if ( > x y ) ( - ( * x x ) ( * y y ) ) ( - ( * y y ) ( * x x ) ) ) )





>> ( square_diff 3 5 )
output>>16.

4.Define a LISP function to compute factorial of any number?


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

>> ( factorial 5 )

output>> 120

Comments

  1. Nice Content I ever get on Lisp. Everything collected at single place.

    ReplyDelete

Post a Comment