Sunday, October 16, 2005

Tiny Lisp course - Lisp in 10 bullets.

With my final year project coming up and with my decision to use Common Lisp for the project for both the compiler and the virtual machine, I had to give my friend and project mate venky a simple introduction to get him to the proficiency of reading and understanding lisp code. So I devised this " Lisp in 10 bullets" so that we can get him to start hacking around common lisp.

Since we are using both scheme and common lisp in the project, this 10 bullets had to be general to include both the popular dialects. Also, since he is not a complete newbie to programming (he's a decent javascript/C/C++ programmer), I don't have the need to explain things like what an 'if' does or what a list is. So here is the list.

  1. In Lisp everything thats written is called a s-expression, which are basically of 2 types. Atoms and Forms.
  2. Atoms are stuff like numbers (10, 234.56, etc..), characters , strings, boolean literals like T (true) and NIL (false) and variable names (called symbols in Lisp, which are associated with other values).
  3. Forms, the 2nd type of s-expressions are always enclosed within paranthesis.
  4. Lisp works by evaluating the s-expression and printing the output, which is the value of the s-expression. Each s-expression's value depends on what it is (atom/form) and what type of an atom or a form it is. The process of finding the value of an s-expression is called evaluation.
  5. Literals (atoms like 12, #\a , "Hey", etc.. basically numbers, chars and strings) evaluate to themselves, i.e 10 evaluates to the number 10. Atoms like symbols evaluate to the value that they are associated to.
  6. Forms are of two types - Functional application and Special forms. And they are always enclosed within paranthesis (this is just rule 3 again, but to drive home the point!)
  7. Special forms are written as (special-form-name arg1 arg2 arg3 arg4 ... argN) and special forms dictates how and when (even wether) each of arg1 to argN is evaluated. Functional Application is simpler, even though written in the same manner as (function-name arg1 arg2... argN), it works by evaluating all the values from arg1 to argN first and then passing them to the function 'function-name'.
  8. Functions in lisp are a bit different and there is a 2 step process to work with them. First the action of creating a function which is done using the lambda special form and then process of associating the function with a name. (i.e, its upto you to assign functions a name, you can leave it if you don't need it).
  9. Lisp has a data structure called the list, whose written literal representation is indistinguishable from that of forms. Its enclosed within paranthesis and its elements seperated by whitespaces. So basically you can see any lisp program as a bunch of atoms (which are data anyway) and lists. This means that lisp code and lisp data are indistinguishable.
  10. Since code is indistinguishable from data, you can write programs to change/generate code because its basically working by modifying lists. Since Lisp provides you with an eval function you can evaluate your generated code at runtime, and Lispniks call this feature 'Macros'.

Hmm, thats my simple 10 bullet guide to lisp, hope it was helpful to all those readers who are interested.

Update 1: As far as point 10 goes, thats not what Lispniks call macros, Macros are simply programs which change/generate code, but this happens during compile time. (however the point is still valid, you can evaluate your code at runtime using eval.)

Signing Off,
Vishnu Vyas

8 comments:

- Intercodes said...

Well, thanks for taking efforts in teaching me LISP in such a short time and inducing interest in LISP programming. It is good to see you share around your didactic skills to the blogging world.(Although, I'm not a decent javascript programmer!:)).

I never felt like I was Joey and you were Phoebe while you were teaching LISP :D ....

To Everyone: Must read for LISP starters.


Cyz

Anonymous said...

Just a quibble with point 2. Symbols are not just variable names, and calling them such loses some of what makes lisp so unique and strange. It would be more accurate to say that symbols are just that, symbols, which can be bound to values.

vishnuvyas said...

Hello anonymous,

Totally agreed.
Yeah, i know they aren't, but its easier for newbies who aren't used to what symbols to first identify with variable names in the begining, this is just a small tutorial after all.

Suchi Yeukie said...

Suchi Yeukie like to post for you about bad credit repair mortgage loan fix repair credit bankruptcy foreclosure equity equifax experian transunion dispute May be you have bad credit. If do then try site today :)

uiyui said...

However mean your gold in wow life is,buy wow gold meet it and live it ;wow gold cheap do not shun it and call it hard names.It is not so bad as you are.It looks poorest when you are richest.The fault-finder will find faults in paradise.wow gold kaufen love your life,poor as it is.maple meso You may perhaps have some pleasant,thrilling,maplestory power leveling glorious hourss,even in a poor-house.The setting sun is reflected from the windows of the alms-house as brightly as from the rich man's abode;the snow melts before its door as early in the spring.sell wow gold I do not see but a quiet mind may live as contentedly there,cheap mesos and have as cheering thoughts,as in a palace.The town's poor seem to me often to live the most independent lives of any.May be they are simply great enough to billig wow gold receive without misgiving.Most think that they are above being supported by the town;powerlevel but it often happens that buy maplestory mesos they are not above supporting themselves by dishonest means.which should be more disreputable.Cultivate poverty like a garden herb,like sage.Do not trouble wow powerleveln yourself much to get new things,maple mesos whether clothes or friends,Turn the old,archlord power leveling return to them.Things do not change;we change.Sell your clothes and keep your thoughts.

jacquesm said...

minor nitpick, it's parenthesis

jimmychooshoes said...

mcqueen shoes
alexander mcqueen heels
Alexander McQueen Store
Jimmy Choo sale
Replica jimmy choo shoes
manolo blahnik online store
manolo shoes
manolo blahnik sale
replica christian louboutin shoes
discount christian louboutin shoes
alexander mcqueen shoes
jimmy choo outlet
jimmy choos
jimmy shoes
Manolo Blahnik Shoes
christian shoes
Alexander McQueen Geometric leather pumps
Alexander McQueen Band leather sandals
Alexander McQueen Faithful Bootie
Jimmy Choo Embossed leather sandals

Friday said...

The christian louboutin store was designed for the ladies. Since the louboutin heelswas born, the ladies life become colorful.The elegant pattern, the delicate style all mold the ladies perfect leg profile. The christian louboutin evening pumps are the god's masterwork. Who invited the christian louboutin pumps? Seldom people knew, but I think every lady would be grateful for him. Among the countless pumps, the christian louboutin peep toe is the most outstanding ones. Flowers in the spring of 2011 creeping, up from hair to clothes continue to footwear, have had a brilliant up. In such a glamor, spring and summer flowers now here. Romance is a woman's mood, exquisite flowers just right of expression in our gestures, the woman, how can we not love the romantic temperament so that they distributed the flowers do? 2011 flowers bloom will enjoy different poses! The Christian Louboutin 2011 Sandals also can adds the hright of the ladies, it bring surprise to the short lady. Especially the red sole of the louboutin heels, magic and sexy, many ladies are crazy. The red sole, the first feature of the Christian Louboutin heels.