April 26, 2011

Alisp in wonderland

Lisp is great, I love it. It is so different from everything else that it just astonishes you. Everybody should learn lisp. If not for any other reason, then just to widen their perspective in programming, and finally to regain their respect to the language they are currently programming.

Lisp is for list processing, naturally. But that's not the point. Where most languages - so called procedural languages - say what should the computer do, in which order, and in which conditions, Lisp concentrates on rules on evaluation: what each variable evaluates into in which conditions. And finally, your program concludes in one root element, or variable, which is then evaluated by running the program.

Because everything in Lisp is about evaluation and there is no sequence of doing things, Lisp as a language does not define structural loops. For todays pgogrammer this is quite odd. (Ok, some lisp variant have some kind of loop macros, but those are some kind of add-on, and that's not the lisp spirit) So when you want to repeat something, you will implement recursion. Therefore you can not talk about lisp without speaking about recursion. I would say half of all lisp functions are calling themselves recursively. Isn't that just ridiculous.

Although Lisp is so different from procedural languages, it has made its impact to popular languages today. Have you ever noticed one particular syntax in C programming language, and wondered why it is so dramatically different from everything else you see in C?

(a ? b : c)

There is a reason. It has been taken from Lisp. That explains its peculiarity. Actually, using above mechanism and recursion, you could make C code look quite a lot like Lisp. Take a look at below example:

int days_per_month(int year, int month) {
    return (month==1||month==3||month==5||month==7||month==8||month==10||month==12 ? 31 :
        (month==4||month==6||month==9||month==11 ? 30 :
            (month==2 ?
                (year>0 && year%4==0 && ( year%100!=0 || year%400==0 ) ? 29 : 28 )
                0
            )
        )
    );
}

Yes this is a wannabe-Lisp C function. Notice the function has only one line, which starts with return and ends with a semicolon. No procedural thinking; the function directly returns something which then evaluates into a decision tree. And no, I did not try to compile it. It's just an example. You are free to comment your findings.

And what else have I done with Lisp then? Years back when I was at school I coded a game AI algorithm that calculated the best possible move in a board game. It was much fun, but the code turned out to be horrible. Maybe all lisp code turns out to be that. If I remember correctly, my professor gave me a rather high grade for this one, but maybe he was just fond of board games.

2 comments:

  1. Many Lisp'ers are now in "ClojureLand" where Lisp trees are growing on top of Java hills (libs). :) Couple of monts ago, I got one job enquiry to join a company that writes Clojure, which was a small suprise. So one benefit of Clojure knowledge is that you might actually get paid when writing it :)

    ReplyDelete
  2. Ha - a Lisp runtime implemented by Java that runs on Java runtime. That sounds at the same time fascinating and pervert. I have to say I was completely unaware of all that. But at the same time it makes a lot of sense. I need to get me one of those to play with!

    ReplyDelete