Академический Документы
Профессиональный Документы
Культура Документы
Submit Tips
Search
HOME
REVIEWS
HOW-TOS
CODING
INTERVIEWS
FEATURES
OVERVIEW
BLOGS
SERIES
IT ADMIN
Search for:
Search
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. http://c2.com/cgi/wiki?InterpretedLanguage 2. http://www.catalysoft.com/articles/goodAboutLisp.html
Follow
+2,497
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
Popular
Comments
Tag cloud
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.
Object-oriented
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 >
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.
Quote
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 )
setq
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
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
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
Previous Post
Next Post
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
C o m m e n t fe e d
Su b s cri b e vi a e m a i l
Reviews
How-Tos
Coding
Interviews
Features
Overview
Blogs
Search
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
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.