Thursday, January 22, 2009

Accumulator Generator in Common Lisp

The following code helps me understand Accumulator Generator in Common Lisp. I am still not used to lisp's true meaning. I often need to struggle to understand a little piece of lisp code. So I write down the code.

(defun foo (n) (lambda (i) (incf n i)))
(setq starting 10)
(setq acc (foo starting))
(funcall acc 1)

For a complete description of Accumulator Generator, refer to Accumulator Generator

Wednesday, January 21, 2009

"define-syntax" and "syntax-rules" in Scheme

Scheme has a macro mechanism which allow us to extend the Scheme language syntax. The description in Revised5 Report on the Algorithmic Language Scheme is precise. But the description has a mathematical flavor.

I refer to the book The Scheme Programming Language. It uses the following example to explain "define-syntax" and "syntax-rules". The following code is to define let. let is defined as (let <bindings> <body>) in Revised5 Report on the Algorithmic Language Scheme.

(define-syntax let
(syntax-rules ()
((_ ((x v) ...) e1 e2 ...)
((lambda (x ...) e1 e2 ...) v ...))))

I still find the description hard for me to understand because I am very new to Scheme. After some time of struggling, I find the following way to understand it. (let <bindings> <body>) is just like a function invocation. bindings create a local binding scope. In some sense, it binds formal parameters to actual parameters. Take (let ((x 1) (y 2)) (+ x y)) as an example, x is bound to 1 and is bound to 2. body is just like the body of an function. (+ x y) will produce 3.

Back to the "define-syntax" example, we see that ((lambda (x ...) e1 e2 ...) v ...) define what we have described as function invocation. For (_ ((x v) ...) e1 e2 ...), we can see that (x v) indicate a binding. ((x v) ...) indicates any number of bindings. e1 indicates a expression in the body. e1 e2 ... indicates all the expressions in the body.

Sunday, January 18, 2009

Great Programming Book

This book (Programming Language Pragmatics) by Scott is really great. Everyone who want to have a complete and deep understanding of programming languages should read this book. I bought this book from US. It costs me more than $60 which is a big amount for a Chinese citizen. Although I have not finished it yet, this book has taught me much.