网络书屋(Web Reading Room)

A blogging framework for hackers.

Think in the Variable and Macro in the Common Lisp

As you all know we can use defun to define a function in the common lisp,

1
2
3
4
(defun add (x)
    (+ x 1))
(defun foo (a b c)
    (list  a b c))

Definition above is the most essential method to create a function. Due to the

convience we will have to think about the variables and functions again.

Writing thinking path: from thinking the variables to functions and get the knowledge of the common lisp.

I. Variables

We we execute one function ,we should let the interpreter know:

  1. call the Mac first? or call the compiler first?
  2. Mac will not get the value of the variables ,but change the expression to another forms
  3. But compiler will directly get the value of the variables.

Variables can be shown with &key,&optional,&rest,&body forms etc.

keyword variables

The variables start with colon( it means :) is the keyword variables, but there are exception,

such as in the defsystem(asdf grammer), you can depends on other system (depends on :macro-utilities)

here macro-utilities is called as a system,not the keyword variables.

1
2
(defun foo (&key a (b 20) (c 30 c-p))
    (list  a b c c-p))

optional variables

1
2
3
4
5
6
7
8
9
10
11
12
13
14
(defun foo (a &optional (b 20) (c 30 c-p))  ;;c-supplied-p

    (list  a b c c-p))

;; it means the variables b and c is optional ,if b is not written in the
;; the parameter list, the interpreter will set b = 20,similary  c.

(defun make-rectangle (width &optional (height width)) ...)

;;; the rectangle will set height =width when height is not supplied.

(defun foo (a &optional (b 20) (c 30 c-supplied-p))

    (list  a b c c-supplied-p))

rest variables

1
2
3
4
5
6
7
8
9
(defun format (stream string &rest values) ...)

(defun + (&rest numbers) ...)

(defmacro when (condition &rest body)
    `(if (not ,condition) (progn ,@body) ;; The difference between , and ,@ in the `() is ,@ will delete the brace more than once 

    ;; (if condition then-part [else-part])
)

body variables

1

macro definition

I think Mac(Macro-> defmacro) is a form to change the input of the variables.Actually it is

most powerful tool in the lisp programming

1
2
3
4
5
6
(format t "hello world")

(defun backwords (str)
    (reverse str))

(backwords ("hello world" t format))  ;; take care not (backwords '("hello world" t format))

So when you execute the backwords macro, the interpreter will tell the Mac to do the works.

When he finished his work. The compiler will continue execute the result of the Mac. That

is the process of the macro function.

II. Functions

Orient Object Programming’s CLOS is to tell you how to rearrange the functions and variables into one class.

1

III. Format function

format is a most powerful decorate function to output in the common lisp.

so every field in the format function , you should take note ~ ,such as ~a, ~{~}, ~%, ~:a, ~r,~d etc

1

added-later