Archive for September, 2007|Monthly archive page

Comments in Lisp

I had to do some searching to figure out the right way to do comments in Common Lisp. I knew the semicolon was used as a comment, but I wasn’t sure if there was more to it. From what I’ve read, there is a style that is followed. Here is the best example I found that explained it well. I found it at this link: Indenting Common Lisp and more

A single semicolon is used for a comment concerning a single line of code, and located on the same line as the code, for instance:

  (if (< (g x) 2)     ; is it sufficiently small?
      (top-level x)   ; if so, abandon everything
      (h y))          ; otherwise try again

Two semicolons are used for a comment that cover several lines of code. The comment line is lined up the the code lines, and precedes them, like this:

  (when (< (g x) 2)
    ;; reinitialize and abandon everything
    (setf *level-number* 0)
    (top-level x))

Three semicolons are used for comments that explain a function. Such comments always start in column 1, like this:

  ;;; Compute the amount of space between symbols
  ;;; as a list of floating point values.
  (defun compute-spaces (symbols)
    (mapcar #'compute-single-space symbols (cdr symbols)))

Lisp on 64

After my last post I wanted to check to see if there was a Lisp interpreter available for the Commodore 64. I found one! It is called Micro-Lisp. Check out the screen shot to see it in action.

Lisp on the Commodore 64

Simply BASIC

I learned about programming on a Commodore 64 over twenty years ago. I still remember working through the BASIC programming examples in the user guide. So I thought it would be fun to start with Common Lisp this way and keep it simple.

When I first started learning BASIC, I thought loops were so cool. At that time, in some stores, they would have computers setup for display. Computers like the Commodore 64 and 128. I thought I was the coolest kid around because I could walk up to one of them and write a quick loop to print my name all the way down the screen over and over. (Hey, I was a kid, what can I say!)

So I decided to write a Common Lisp function that would print “Hello, World!” ten times down the screen. It looks like this.

(defun hello-world ()
    (dotimes (x 10)
        (format t “Hello, World!~%”)))

When this function is executed, the output looks like this.

Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
NIL

You can consult some of the references I have mentioned in my previous posts to figure this out pretty easily. The format function can look visually strange I think. Especially when you see more advanced expressions. For instance, the ~% that follows Hello, World! inserts a new line, but it looks strange.

With all of this talk about BASIC and the Commodore 64, I decided to fire up my VICE C64 emulator and write the same program in BASIC. (Yeah, crazy, I know!) So here it is.

10 FOR X = 1 TO 10
20 PRINT “COMMODORE 64″
30 NEXT X

Then when you run this you get.

COMMODORE 64
COMMODORE 64
COMMODORE 64
COMMODORE 64
COMMODORE 64
COMMODORE 64
COMMODORE 64
COMMODORE 64
COMMODORE 64
COMMODORE 64

There you have it. I know it is simple, but it was a fun and easy way for me to achieve success with my first Common Lisp program.

Adventure into REPL

Probably the largest hurdle I have been jumping so far is the interactivity of programming in Common Lisp. I’ve never used anything quite like it. Enter the REPL! (That sentence sounds like the Bruce Lee movie, Enter the Dragon, get it? *wink*) It stands for Read, Evaluate, Print, Loop. Do yourself a favor and read Chapter 2 of Practical Common Lisp to help you start using the REPL.

In other languages I would usually concentrate on writing my program as a whole, save the source file and then compile it or run it. In Common Lisp, you can actually code and test a function at a time. Open two windows within Emacs with C-x 2. In one of them open your program to write your code. In the other have the REPL ready. After you write a function for example, place your cursor right after the function and type C-c C-c. That is Control-c Control-c. The results should show up immediately in the REPL. By doing this, you can test snippets of code you write very quickly and easily. It is a difficult feature to wrap your mind around if your not accustomed to it. The REPL is very cool to use though, I like it.

Study Lisp

Well, I’ve been reading everything I can about Lisp for months. I’ve read about 80% of the book, Practical Common Lisp by Peter Seibel. It is definitely the best introduction to Common Lisp out there. It is available on the Web too. I have been reading the book, ANSI Common Lisp by Paul Graham. There are a couple of other books available on the Web that I’ve been reading parts of too. One is Successful Lisp by David B. Lamkins. The other is Common Lisp: A Gentle Introduction to Symbolic Computation. It is wonderful to see great books like this available online.

There are many other resources available online. I read Planet Lisp daily. I read the news group comp.lang.lisp regularly. It can be entertaining sometimes in a weird sort of way. Lemonodor is another blog I check daily. Since it isn’t included in Planet Lisp anymore, I check it separately. It is a good read! The other day I read Pascal Costanza’s Highly Opinionated Guide to Lisp. He did a great job, I liked it a lot. There is a lot of good information in his guide.

There is a lot of interesting Common Lisp information easily available. I’m now using what little time I have and actually coding. I’m really enjoying it. It is definitely a major change from the Basic style of languages I know. I feel very far behind the veteran Common Lisp coders as a programmer. My skills are in the Basic style of languages and I’m self taught. I first learned how to program Basic on a Commodore 64. I dabbled in Fortran in college. Through the years I’ve learned Foxpro, Visual Basic, VBScript ASP and HTML/XHTML. So Common Lisp is totally different than what I’m accustomed too. But my plan is to become a better programmer, so I think I’m on the right track.

Beginners

There are a couple of things I want to mention regarding Emacs. In my last post I mentioned the quick guide from the #lisp channel. In that guide the code is given to setup Emacs with SLIME and your Common Lisp implementation. This is important to know. You place the code in the .emacs file in your home directory. The other item I want to mention is how to start learning Emacs. I found that using the built-in Emacs interactive tutorial was a great introduction for me. I felt much more comfortable after running through the tutorial. To enter the tutorial, open Emacs and then do Ctrl-h and then type the letter t.

I’ve noticed there isn’t a lot of Common Lisp beginner material out there, so that is another reason I started this blog. That is the reason I want to provide some of the basic information I have ran into.

Next Page »