Wednesday, July 23, 2003

Reading R5RS

I'm now reading the official Scheme specification, the R5RS. Some things make more sense in the spec than they did in Fixnum. Also, macros are defined using define-syntax or let-syntax, but Fixnum used define-macro, which I assume is a macro itself. I have not yet figured out how to use define-syntax; my first experiment failed.

I haven't yet reached the part in R5RS about continuations. We shall see if it helps me any.

Tuesday, July 22, 2003

Little Schemer

The Little Schemer arrived yesterday, and I'm now in the second chapter. It starts out very easy, which is what I expected, but my hope is that reviewing these basic concepts will cause them to become more solid in my mind. The novel question and answer approach makes going over the old material more interesting. Actually, although such an approach is novel for a computer science textbook, it is actually a return to the catechism technique that has been used in the Christian Church for two millennia.

Saturday, July 19, 2003

Macro Examples

I realized that I didn't show how to use my macros in the earlier post. For the arithmetic if, I have a couple of examples:
(arith-if test-val

This evaluates to the string "negative", "zero", or "positive", depending on the value of test-val.
(arith-if test-val

(set! accum (- accum 1))
(set! accum 0)
(set! accum (+ accum 1)))
If test-val is negative, the accum variable is decremented, and if it is positive, accum is incremented. If test-val is zero, accum is reset to zero.
(p-for i 1 10 (display i)(newline))
This prints the numbers 1 through 10. It's that easy!

Lost in Continuations

I made it through chapter 8 of Fixnum, but I started to lose interest in chapter 9. Chapter 8 got me to macros, which seems to have completed the bulk of the language. Chapter 9 shows how to do structures, which aren't really part of the language, but are added with macros. The same thing is done in chapter 10 for tables and chapter 12 for objects.

By chapter 12, I'm just skimming through so I get completely lost in chapter 13, which is about call-with-current-continuation, also known as call/cc. I really don't get what it does, except that it has to do with jumps. Not understanding call/cc ruined me for much of the rest of Fixnum. While the fast pace of the first half of Fixnum was good for me, the second half moved too fast and lost me.

That's ok, though. I learned enough to be intrigued with Scheme; I will look to some other resources to continue my learning. Later I can come back to Fixnum and will hopefully do better.

I've ordered The Little Schemer, which has received great reviews. My understanding is that it starts out very basic, but gets pretty difficult at the end. It has been praised as one of the best written computer science teaching books, so I'm looking forward to it.

Thursday, July 17, 2003


I'm learning macros now. They take a little getting used to, but I've written a couple of them to recreate some control structures from other languages. First is the arithmetic-if from old FORTRAN.
(define-macro arith-if

(lambda (test-val neg-branch zero-branch pos-branch)
`(cond ((< ,test-val 0) ,neg-branch)
((= ,test-val 0) ,zero-branch)
((> ,test-val 0) ,pos-branch))))

Next is a for loop from Pascal or BASIC (without a step option).
(define-macro p-for

(lambda (counter start limit . body)
`(let loop ((,counter ,start))
(when (<= ,counter ,limit)
(loop (+ ,counter 1))))))

Tuesday, July 15, 2003

Learning Scheme

Yesterday I started to learn the Scheme programming language, and this morning I got the idea to journal the learning process in a weblog.

Scheme is a variation or dialect of the Lisp programming language. I've been interested in Lisp for about 17 years, ever since I did some exploratory research into artificial intelligence. Unfortunately, I couldn't run Lisp on any of the computers to which I had access at that time so I didn't learn it then. My work took me in other directions and other programming languages, so I never had a clear professional reason to learn it.

As I got into weblogs early last year, I also got exposed to people like Dave Winer who advocated the use of scripting languages to increase productivity. I dabbled in Ruby, started to learn Python, and brushed up on my ancient knowledge of Perl. However much I recognized the merits of these languages, though, I never fell in love with them.

There were a couple of other weblogs that I started reading, Lambda the Utlimate, and Lemonodor. Lambda is about programming languages, with a heavy dose of functional programming. Lemonodor is all about Lisp. These blogs reawakened my interest in Lisp, so eventually I decided to learn it.

Lisp is a language with a lot of variants known as dialects. It has evolved a lot over the years, with different dialects taking radically different approaches. What the dialects do have in common is the heavy use of the list data structure, with programs as well as data expressed as lists. This idea of a program being data has always fascinated me.

Today, there are two heavily used Lisp dialects: Common Lisp, and Scheme. Common Lisp is big and powerful, and Scheme is small and elegant. This is not to say the Common Lisp is without elegance, or Scheme is without power, but in the world of compromises that make up programming language design, Common Lisp seems to emphasize power, where Scheme emphasizes elegance.

Although the design goal of small and elegant attracted me to Scheme, whenever I looked at code comparisons between the two, Common Lisp code was usually the more compact, so I was leaning towards Common Lisp. I downloaded OpenMCL for my Macintosh, as well as some tutorial material, ready to tackle Common Lisp. Unfortunately, OpenMCL does not have an IDE, and BBEdit doesn't really help out for Lisp. Vi has a nice parenthesis matching function, but the real editor for Lisp is Emacs. I have great respect for Emacs, but it doesn't really fit my Macintosh environment. Also, my hope is to use Lisp as a scripting language, not only for personal use, but on my PC at work, too. A nice Common Lisp IDE was going to cost me about $500 for the PC and over $900 for the Macintosh. If I ever need Common Lisp for a serious project, it may be worth it, but I can't justify that kind of money right now.

Then I remembered DrScheme, which I learned about on one of the weblogs. I downloaded it and began going through a tutorial it has for teaching Scheme to experienced programmers called Teach Yourself Scheme in Fixnum Days. I began work on it during my bus ride to and from work yesterday. DrScheme is a nice environment and Fixnum is a good tutorial. Yesterday evening I wrote my first loop using tail recursion, and I fell in love.

I will admit that after one day, I cannot be sure if this is true love, but already I see a beauty in Scheme that attracts me. I've written a long enough introduction, so I will save the specifics for later, and we will see if this initial crush develops into a steady relationship.