You are on page 1of 6

Write For Us

Submit Tips

Subscribe to Print Edition












Lisp: Tears of Joy, Part 4

By Vivek Shangari on September 1, 2011 in Coding, Developers 0 Comments

Search for:


Get Connected RSS Feed Twitter

Lisp has been hailed as the worlds most powerful programming language. But only the top percentile of programmers use it, because of its cryptic syntax and academic reputation. This is rather unfortunate, since Lisp isnt that hard to grasp. If you want to be among the crme de la crme, this series is for you. This is the fourth article in the series that began in the June 2011.
.. For God wrote in Lisp code When He filled the leaves with green. The fractal flowers and recursive roots: The most lovely hack Ive seen. And when I ponder snowflakes, never finding two the same, I know God likes a language with its own four-letter name. .. Partial lyrics of the song God Lives on Terra by Julia Ecklar.
I would like to think these lines speak to all of you out there reading this article the ones whove fallen in love with Lisp, the ones who still dont get what all the fuss is about, and those who need some more nudging to fall off the fence (onto the right, Lisp-loving side). For those of you in the second and third categories, Ive answered a few of your questions below, which might help you along the path to enlightenment. I am standing on the shoulders of giants. Some text snippets are quoted from these two URLs: 1. 2.

LINUX For You on



Lisp is slow because it is interpreted

Common Lisp is not an interpreted language. In fact, there is not even a reasonable definition of an interpreted language. The only two reasonable definitions of an interpreted language that I can think of, are:

1. A language that can be implemented with an interpreter. 2. A language that must be implemented with an interpreter. In the first case, all languages are interpreted. In the second case, no language is interpreted. Sometimes, we confuse interpreted and interactive. We tend to think that whenever there is an interactive loop such as Lisps read-eval-print loop, there must also be an interpreter. That is false. The eval part can very well be implemented with a compiler. Sometimes, the belief is that even though it is possible to implement Common Lisp with a compiler, it is usually done with an interpreter, and hence most implementations are slow. This might be the case for programs written by amateurs, but is seldom the case with systems written by hackers. Almost every Common Lisp system in existence uses a compiler. Most Common Lisp compilers are capable of generating very good, very fast code. Obtaining fast code may require the hacker to add declarations that assist the compiler in optimising the code, such that fast object code is not derived out of nave source code. Also, mortals have a tendency to exaggerate the importance of speed. There are quite a number of applications that use very slow systems, such as Perl, Shell, Tcl/Tk, Awk, etc. It is not true that maximum speed is always required.

Find us on Facebook

Open Source For You

Like 252,949 people like Open Source For You.

F acebook social plugin



Tag cloud

August 13, 2013 13 Comments Diksha P Gupta

India has immense under-utilised talent in the cloud security space

May 6, 2013 6 Comments Priyanka Sarkar

Lisp is not used in the industry

..or, Ive not seen Lisp in advertisements for employment. Lisp is used. Several major commercial software packages are written in Common Lisp or some other Lisp variant. It is hard to know in what language commercial software is written (since the user should not have to care), but there are a few that are well known. Interleaf, a documentation system, is written in Lisp. So is AutoCAD, a system for computer-aided design. Both are major applications in their domains. While not a commercial software system, Emacs is an important system written in Lisp. But even if Common Lisp were not used at all in industry, this would not be a good argument. The level of sophistication of the software industry is quite low with respect to programming languages, tools and methods. The universities should teach advanced languages, tools and methods with the hope of having industry use them one day, as opposed to teaching bad ones that happen to be used today. Students who want training in particular tools that happen to be in demand at the moment, should quit university, and apply for more specific training programs. Lisp was, and is, one of the dominant languages for Artificial Intelligence (AI) programming, but should not be seen as a language that is exclusive to the AI world. The AI community embraced the language in the 1980s because it enabled rapid development of software in a way that was not possible with other mainstream languages of the time, such as C. In the 1980s and early 1990s, the emphasis of mainstream software development methodologies was on getting it right the first time, an approach that demanded upfront effort on careful system specifications, and did not allow for changes in specifications during later stages of the software development life-cycle. In AI, software development needed to be much more agile, so that inevitable changes in specifications could more easily be accommodated by iterative development cycles. Lisp is ideal for this, as it can be tested interactively and provides for concise, quick coding, using powerful high-level constructs such as list processing and generic functions. In other words, Lisp was ahead of its time, because it enabled agile software development before it became respectable in mainstream software. Besides, whether a language such as Common Lisp is used in the industry depends a lot more on the individual student than on industry. There is a widespread myth among students that the industry is this monolithic entity whose tools and methods cannot be altered. In reality, the industry consists of people. Whatever the industry uses is whatever the people working there use. Instead of refusing to learn sophisticated tools and techniques, the student can resolve to try and become one of the forerunners in the industry, after graduation.

PHP Development: A Smart Career Move

June 20, 2013 3 Comments sophie-samuel

New and amazing features of Linux

June 20, 2013 3 Comments Priyanka Sarkar

What it Takes to be an Open Source Expert

May 6, 2013 1 Comments Deepti Sharma

A Simple guide to building your own Linux Kernel

Recursive or iterative?
Unlike some other functional programming languages, Lisp provides for both recursive and iterative programming styles. As an example, consider writing a function to compute the factorial of a positive integer n. The factorial, written n!, is the product of all integers between 1 and n; i.e., n! = n(n-1)(n-2)21. A function to compute this number can be written in a recursive style as follows:
( d e f u nf a c ( n ) ( i f( =n0 )1 ( *n( f a c( -n1 ) ) ) ) )

You should read the definition of the function as follows. If n is zero, then return 1; otherwise return the product of n and the factorial of n-1. The same function can be written in an iterative style as follows:
( d e f u nf a c ( n ) ( l e t( ( r e s u l t1 ) ) ( d o t i m e s( in ) ( s e t qr e s u l t( *r e s u l t( +i1 ) ) ) ) r e s u l t ) )

This function uses l e tto set up a local variable called r e s u l twith an initial value of 1. It then performs n iterations of a central loop that each time multiplies the result by the loops counter variable (one must be added to this counter, as d o t i m e scounts from zero). Finally, the value of the r e s u l tvariable is returned as the result of the function.

Like many other modern programming languages, Common Lisp is object-oriented. In fact, it was the first ANSI-standard object-oriented language, incorporating CLOS (the Common Lisp Object System). CLOS provides a set of functions that enable the definition of classes, their inheritance hierarchies and their associated methods. A class defines slots whose values carry information about object instances. The class definition can also specify default values for slots, and additional non-trivial behavioural mechanisms (such as integrity checking) performed at object creation time. As an example of object-orientation in Lisp, consider the definition of some shapes, which can be either circles or rectangles. A shapes position is given by x and y coordinates. Further, a circle has a radius, whereas a rectangle has a width and a height. It should be possible to compute the area of circles and rectangles. A working set of definitions is given below:
( d e f c l a s ss h a p e( ) ( xy ) ) ( d e f c l a s sr e c t a n g l e( s h a p e ) ( ( w i d t h: i n i t a r g: w i d t h ) ( h e i g h t: i n i t a r g: h e i g h t ) ) ) ( d e f c l a s sc i r c l e( s h a p e ) ( ( r a d i u s: i n i t a r g: r a d i u s ) ) ) ( d e f m e t h o da r e a( ( o b jc i r c l e ) ) ( l e t( ( r( s l o t v a l u eo b j' r a d i u s ) ) ) ( *p irr ) ) ) ( d e f m e t h o da r e a( ( o b jr e c t a n g l e ) ) ( *( s l o t v a l u eo b j' w i d t h ) ( s l o t v a l u eo b j' h e i g h t ) ) )

Using these definitions, a rectangle (instance) can be created with m a k e i n s t a n c e , for example, with a width of 3 and a height of 4:
>( s e t qm y r e c t( m a k e i n s t a n c e' r e c t a n g l e: w i d t h3: h e i g h t4 ) ) # < R E C T A N G L E@# x 8 a e e 2 f 2 >

Its area can be computed by calling the method area:

>( a r e am y r e c t ) 1 2

Below, Ive attempted to address in my clever-yet-so-simple manner of explaining concepts (I hope thats not laughter I hear) some of the topics my very-soon-to-be fellow Lispers have had trouble understanding from books.

Lisp evaluates everything. Sometimes you dont want Lisp to do that; in situations like these, q u o t eis used to override Lisps normal evaluation rules. This q u o t eis a special operator. It has a distinct evaluation rule of its own, which is to evaluate nothing. It takes a single argument, and returns it verbatim.
>( q u o t e( +72 ) ) ( +72 )

This may seem to be a mundane function, since it does nothing. But, the need to prevent Lisp evaluating something arises so often that a shorthand notation is provided for quoting expressions. Common Lisp defines '(a single quote or apostrophe) as an abbreviation for q u o t e . Be careful not to substitute q u o t ewith `(backquote character). ` (referred to as backquote by Common Lisp programmers, and quasiquote by Scheme programmers) has a different interpretation, which I will cover in forthcoming articles.
>( ' ( t h el i s t( +235 )s u m s3e l e m e n t s ) ) ( T H EL I S T( +235 )S U M S3E L E M E N T S )

Lisp programs are expressed as lists. This means that Lisp programs can generate Lisp code. Lisp programmers often write programs to write programs for them (such programs are called macros). The quote function is crucial to engineer this functionality. If a list is quoted, evaluation returns the list itself; if it is not quoted, the list is treated as Lisp code, and evaluation returns its value.
>( l i s t' ( +011235 ) ( +011235 ) ) ( ( +011235 )1 2 )

Atom, list, and predicates

Things inside a list things that are not themselves lists, but words and numbers (in our examples so far) are called atoms. Atoms are separated by white space or parenthesis. You may use the term atom to refer to just about any Lisp object that is not viewed as having parts. Atoms like 2.718 and 42 are called numeric atoms, or just numbers. Atoms like foo, bar, and + are called symbolic atoms, or just symbols. Both atoms and lists are called symbolic expressions, or more succinctly, expressions. Symbolic expressions are also referred to as s-expressions, s being short for symbolic, to distinguish them from m-expressions, m being short for meta. Symbols are sometimes referred to as literal atoms. You can check if an s-expression is an atom or not by using predicates functions that return true or false when testing a value for a particular property. In Lisp, false is indicated by N I L , and true is often signalled by the special symbol T , but anything other than N I Lis considered to indicate true. Similarly, the function l i s t pdetermines whether something is a list.
>( a t o m' ( f u n c t i o n a l ) T >( a t o m' ( abc ) ) T >( l i s t p' ( abc ) ) T >( l i s t p2 . 7 1 8 ) N I L

Most programming languages provide a special notation for assignment. Lisp does no such thing. Instead, Lisp uses its routine workhorse, the function. Lisp has a special function, called
s e t q , that assigns the value of one of its arguments to the other argument. To accomplish the

equivalent of x < -4 7in Lisp, we write the following:

>( s e t qx4 7 ) 4 7 >( s e t qy( +47 ) ) 1 1 >( +3( s e t qz( *37 ) ) ) 2 4 > z 2 1 >( s e t qm y m e d i u m o f e x p r e s s i o n" L i s p " ) " L i s p "

s e t qgives each variable symbol the value of the corresponding value expression. The s e t q

form can take any even number of arguments, which should be alternating symbols and values. It returns the value of its last value.
>( s e t qm o n t h" A u g u s t "d a y0 4y e a r2 0 1 1 ) 2 0 1 1 >d a y 0 4 >m o n t h " A u g u s t "

Lisp tries to evaluate arguments before applying a function. When we evaluate a s e t q , the symbol (in our example, x, y, and z) may not be defined. This should cause an error the only reason it does not is because s e t qis an instance of a special function. Lisp may handle the argument of each special function idiosyncratically. s e t qdoes not cause its first argument to be evaluated; and the second argument is subject to normal treatment. Fortunately, there are only a small number of special functions in Lisp. Whats next? Drum-roll (and if this were a television series, there would be fireworks going off now) Macros. After weeks of reading about the power of macros, you will finally learn how to write them. For those of you who dont know what Im talking about, think Agent Smith (the darkglasses-wearing AI program in the movie Matrix) turning into a self-replicating computer virus. If, for a second, you ignore the fact that he was the antagonist in the movie, and that nobody likes computer viruses, and focus instead on the concept of programs that can actually write programs, I am sure the start of next month will see you parked at a newsstand waiting for the next edition of LFY. And I look forward to it as well, as that will take us one step closer to our

plans of world domination.

Related Posts:
Lisp: Tears of Joy, Part 6 Lisp: Tears of Joy, Part 5 Lisp: Tears of Joy, Part 10 Lisp: Tears of Joy, Part 3 Lisp: Tears of Joy, Part 7
Tags: academic reputation, Agent Smith, AI, Artificial Intelligence, AutoCAD, Common Lisp, Emacs, hack, interactive loop, Interpreted language, interpreter, Julia Ecklar, LFY September 2011, Lisp, Lisp: Tears of Joy series, matrix, object-oriented programming, OO, perl, programmers, Programming Language, software development, Tcl/Tk

Article written by:

Vivek Shangari
The author is a hard-core hacker, if there ever was one. He always thinks programs, and holds a piece of code in his head all the time. His favourite past-time is to stop random strangers on the street and start talking about the benefits of open source over proprietary software, till they agree to switch sides, or threaten to jump off the nearest building. Connect with him: Website

Previous Post

Next Post

Modify-function-return-value hack! -- Part 2

FOSS Trainer Powers Up 170 College Labs in Tamil Nadu!



What's this?

Pastor Mocked For His "Biblical Money Code" Moneynews Don't Get Alzheimer's: Here's What May Cause It Newsmax Health President Obama Reveals Favorite Food First To Know Protect Yourself in a Divorce: 9 Things You Must Do Citi Women & Co.

India has immense under-utilised talent in the cloud 13 comments Getting Your First Job Code Sport
1 comment 1 comment

File Systems A Semester Project-II, Part-19 6 comments

0 comments Leave a message...

Newest Community Share

No one has commented yet.

C o m m e n t fe e d

Su b s cri b e vi a e m a i l








Popular tags
Linux , ubuntu, Java, MySQL, Google, python, Fedora, Android, PHP, C, html, w eb applications , India, Microsoft, unix , Window s , Red Hat, Oracle, Security , Apache, xml, LFY April 2012, FOSS, GNOME, http, JavaScript, LFY June 2011, open source, RAM, operating systems

For You & Me Developers Sysadmins Open Gurus CXOs Columns

All published articles are released under Creative Commons Attribution-NonCommercial 3.0 Unported License, unless otherw ise noted. LINUX For You is pow ered by WordPress, w hich gladly sits on top of a CentOS-based LEMP stack.