Monday, July 17, 2006

Insertion Sort

Its been a while since I posted any Lisp content and I have not been in touch for long. For anyone trying to get in touch, I still don't have access to my Yahoo account.

Well, I was trying a simple insertion sort algorithm directly from a book.


(defun insertion-sort (A)
(do ((j 1 (+ j 1)))
((>= j (length A)) 'done)
(let ((key (aref A j))
(i (- j 1)))
(while (and (>= i 0) (> (aref A i) key))
(progn
(setf (aref A (+ i 1)) (aref A i))
(setf i (- i 1))
(setf (aref A (+ i 1)) key)
(print x))))))


The while is just a macro copied out of ANSI Common Lisp and very basic (no gensyms etc). Just for reference, it is


(defmacro while (test &rest body)
`(do ()
((not ,test))
,@body))


Executing it as follows (Followed the algo by the book).


CL-USER> x
#(5 2 4 6 1 3)
CL-USER> (insertion-sort x)

#(2 5 4 6 1 3)
#(2 4 5 6 1 3)
#(2 4 5 1 6 3)
#(2 4 1 5 6 3)
#(2 1 4 5 6 3)
#(1 2 4 5 6 3)
#(1 2 4 5 3 6)
#(1 2 4 3 5 6)
#(1 2 3 4 5 6)
DONE
CL-USER> x
#(1 2 3 4 5 6)


Its amazing, how much one forgets in a few months/years.

Saturday, June 03, 2006

Rocket & Ricotta Special Rolls


Well, I am back after a long sojourn. Its been more than a year since I blogged. Sorry to people who have been reading and following along.

We made an interesting looking meal today and it looked fairly ok and tasted quite good. It appears to be pretty healthy too.

I will list all the ingredients here and you folks can try to figure out what went into it. Its not as simple as it looks as it does comprise of a lot of leftovers which were not that easy to make. So if someone wants the recipe then, let me know. I may send it to you if I can remember how.

Ingredients
Tortilla Wraps,
Spinach, Small Radish, Cucumber, Onion, Yellow Pepper
Rocket Salad - Lollo Bionda Lettuce, Red Batavia Lettuce, Rocket, Mizuna
Garlic, Olive Oil, Soy Sauce, Cumin Seeds, Garam Masala, Maggi Hot & Sweet Sauce
Low Fat Cheese Slices, Ricotta Cheese
Eggs and Egg Whites

Tuesday, August 31, 2004

Hear me squeak

Squeak is a highly respected free implementation of Smalltalk based on the original Smalltalk 80 with names such as Alan Kay and Dan Ingalls (from the original Smalltalk project) involved in it. The cool thing about Squeak is that the code not only works bit-identical across platforms, but also looks the same.

I have always wanted to play with Smalltalk because there are people who I respect and personally know in the software world who do not like to work with any other language. My friend Jason, a Smalltalk hacker had given me the 10 minute introduction a while ago about the 3 types of messages is all one needs to understand.

So I decided, to try it out for real. This is more of a challenge than one thinks, because you have to change your way of thinking. However, some of the tutorials on the web were helpful.

First things first. I hate the default theme. I can't stand it. They really could make it look nicer without my having to tweak the settings. But look and feel was the last thing on my mind. So I followed a few tutorials and came to one conclusion.

I absolutely love the environment. I am not crazy about the language or syntax, even though its nicer than any of the mainstream languages. I would anyday choose Lisp over it. I miss the parens already.

The image saving is probably the best part. CL implementations also have image saves, but really it cannot compare to the way Smalltalk does it. "Inspect" and "Do It" are great, but that is something that I expect from a half decent language anyway.

My ideal development environment would probably be a Common Lisp that is really inspired by Smalltalk environments. As far as I have heard and read about it, Lisp Machines were/are like that some 20 years ago. I do not know if any of the modern Lisp environments are like that.

The Smalltalk System Browser is such an easy to use well thought out tool. I also tried the trial version of Visual Works on Mac OS X. I did not like it as much. It seemed too complicated to me after following the "WalkThru" exercise. One tip to Cincom - GUI walkthroughs suck. Show me the language first. Show me the power of iterating through a collection of objects or something of that nature. Something that lets me write the equivalent or at least close to MAPCAR in Smalltalk. Show me how to accomplish more with less code. I know it does have those capabilities and as a developer, that is what I am waiting to see.

Tuesday, August 17, 2004

Using CL-PPCRE for weblog search

A little while ago, I wrote a search facility for my weblog. This was for personal use, and the code might be very primitive. However, here are the main functions that I used.


;;; This program depends on CL-PPCRE. It should be loaded
;;; as a package first or available in the lisp image

(defun file-to-string (file)
(apply #'concatenate 'string
(with-open-file (str file :direction :input)
(loop for line = (read-line str nil 'eof)
while (not (equal line 'eof))
collect line))))


(defun scan-in-file (srch file)
"scan for a single occurence of the string in the file"
(cl-ppcre:scan srch (file-to-string file)))


;;; Takes a list of pathnames as the arg for dir.
;;; (scan-in-directory "blah" (directory "*.txt"))
(defun scan-in-directory (srch dir)
(let ((files (loop for x in dir collect
(namestring x))))
(remove-if-not #'(lambda (file)
(scan-in-file srch file)) files)))

;;; searches the tree below current dir.
;;; (search-in-tree "question" "*.txt")
(defun search-in-tree (srch wildcard)
(let ((dir (nconc (directory wildcard)
(directory (concatenate 'string "*/" wildcard)))))
(scan-in-directory srch dir)))


;;; Creates a url that a browser can visit
(defun create-url (file-name base-dir)
(let ((base-url (ext:getenv "SERVER_NAME")))
(let ((fpname (pathname file-name)))
(let ((abs-path (pathname-directory fpname))
(name (pathname-name fpname))
(type (pathname-type fpname)))
(let ((p (position base-dir abs-path :test #'equalp)))
(let ((rem-dirs (nthcdr (1+ p) abs-path)))
(let ((rem-path (namestring (make-pathname :directory (cons :absolute rem-dirs)))))
(concatenate 'string "http://" base-url "/" base-dir rem-path name "." type))))))))


If someone is curious enough to try it, its here

Mini Weblog Search Feature

I've used my homegrown CGI library, although this server does have mod_lisp installed, which I would like to play with when I have time.

Increased usage of Smalltalk

Over the last couple of months, I have seen many new Smalltalk jobs here in India. Placement consultants also keep contacting me about opportunities just because I have Smalltalk on my resume. The only reason it is there is because of the description of software for which we were developing a data warehouse. While I do have a passing familiarity with the language, I did not realise that there would be people working on it in India. Intringuing.

Sunday, August 01, 2004

Self Publish Your Books

After looking at this post via Lemonodor via Planet Lisp, I personally feel that more Lisp authors should self-publish their books on the web and serve them as downloadable softcopies. There are many sites that let people sell e-books, and I would anyday buy something that is good quality writing like Peter Siebel's upcoming book.

This will help keep more of the money in the authors pocket's (which they deserve) and hold other rights to the content that they create. There are many reasons why I think this is a better approach than using a publisher especially in a small and niche market segment like Lisp related material.

Been a bad week

This last week has been horrible. I managed to corrupt my hard disk to the point where my Powerbook would not even boot. This happened just before I took my set of data backups. All the code, I could rewrite but to go back to the himalayan ranges in the east and take pictures of nature and buddist monasteries again would have been some effort.

Luckily I had YDL on a partition which came in handy, to install a new copy of Panther and access my hard disk. I managed to get all my backups done, and re-installed the OS. Now I am piecing things back together. Lets see how it goes.

Monday, July 12, 2004

This is better than that

It is really interesting when people highlight different parts of the same article when they interest them. Lispers and Smalltalkers usually get along quite well. I have hardly ever seen one say a bad thing about the other. I found this rather interesting, especially the pick of quotes. This refers to quotes picked out from Ted Leung's post.

From a primarily Lisp blog.

Lisp is the number one programming language idea of all time. Smalltalk's contribution was to build encapsulation on top of the ideas in Lisp. Someone asked a question about the market voting or something like this. Kay's reply went something like this: Most people don't understand Lisp -- does that make it bad? Most people understand Maxwell's equations -- does that make them bad? One of the things that Kay is doing now is just going around and giving talks to remind people of the great ideas in Lisp and Doug Engelbart's work


From a primarily Smalltalk blog.

Much as I love Lisp, it seems to me that the Smalltalk community, led by folks like Kay, are continuing to demonstrate a convincing agenda for forward progress, while the Lisp community is perennially struggling with basic infrastructure issues like which dialect of Lisp/Scheme, which windowing environment, etc. Perhaps this is due to the conception of Smalltalk as a system, in addition to a language.


Heh?


Sunday, July 11, 2004

Ayurvedic Massage

So I decided to get one of the Ayurvedic massages from one of the Ayurvedic clinics here. Ayurveda is India's oldest medical science and probably pre-dates every other kind of medicine including the Chinese. The state Kerala in the south of India has become a very popular tourist destination just because of Ayurvedic treatments.

Anyway, it is quite an ordeal. The massage is extremely rigorous and tiring (not like the usual relaxing massages). They use heated medicated oils which they pour on different parts of the body depending on the treatment which is followed by a rigorous massage. This goes on for at least half an hour. Then they wipe you down steamed towels which smell like camphor.

However, it is very rejuvenating. Google for 'Kerala Ayurvedic Massage' for interesting articles and pictures on the subject. If you have not tried this, it is recommended at least once. Besides, a trip to Kerala, India can be very relaxing. Its all coastal with nice clean beaches and backwaters on which you can rent a two bedroom boat for 2-3 days with a personal chef.

Kerala is the only state in India that has achieved 100% literacy and people prefer to communicate in English, besides their local language Malayalam.

Writing LTK Extensions

Tcl/Tk have some nice extension packages like Tix, BWidgets, TkTable, Tclodbc etc. Most of them can be used from the tcl (tclsh) or tk (wish) shells. I asked Peter about adding extensions to Ltk, and he has included a chapter in the latest release of Ltk (0.8.5) AFAIK. Two thumbs up to this guy for documentation and support. He has included an example for one Tix widget.

I have already started working on Ltk-odbc which will wrap tclodbc and return Lisp lists for data. It needs a lot of work as well as documenting but it has started. Watch for more on this. The next would probably be Tktable after this.