Monday, March 13, 2006

I see parse trees!

I have a confession to make. I've been playing with scheme again. The trouble is that languages like Scheme and Lisp are just so interesting. My biggest problem is I'm not thinking like a compiler yet. I've been doing so much procedural programming that I'm not really ready to jump straight into generating parse trees.

To skip the code scroll down to "END CONCLUSIONS".

Take for instance a factorial function I just wrote:
(define (mfac num)
(if (= 1 num)
(* num (mfac (- num 1)))))

In C I could write this a couple of ways...
int mfac(int num){
return num == 1 ? num : num * mfac(num -1);
int mfac(int num){
if(num = 1)
return 1;
return mfac(num - 1);

Now these map pretty well. It's just doing other more procedural things that is harder. I'm just not thinking in parse trees well enough yet. Take the following:
char *str = "This is a string";
printf("After += 5 \"%s\"\n",str);
I can see that I need to use (let ((str "This is a string")) (print str))
The trouble is doing things like the pointer addition and the subsequent call to (print ...).

I get errors with the following:
(let ((x 2))
(print x)
(print (* x 2))

Update: I think I figured out why (let ...) wasn't working. I was using the "Advanced Student" language with DrScheme. I tried "Standard R5RS" and it started working. Oops... I even tried (let) with CLisp (Yes, I know Lisp and Scheme aren't the same) and it worked as expected there too.

C would look like this:
int x = 2;
printf("%i\n",x * 2);


It doesn't seem to want to work. There is something I don't understand yet. It's obvious. I'm getting close and it's more of a syntax thing than anything else. Suffice it to say this is one of the most fun/hardest programming languages I've ever tried to learn. The coding style I used when doing the first C implementation of mfac() above came to me after I started learning Scheme.

People look at me funny and wonder why it is that I can get excited about a language. I guess it's because it's taking everything I thought I knew and shifting it. I still write programs in the same languages; they just don't look the same to me anymore.


1 comment:

NicKAD said...

I like your blog.
It's great to see your diving into lisp. I am dabbling down the side street of AutoLISP. (I think it is only a block or two away.)

Speaking of being a block or two away it looks like your new place is about that distance from my parents home.

I think I'll stop on by the pi party tomorrow.


I am about to launch a blog too.