[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

partial solution for project 0 and some useful macros for lisp



The solution to some problems of project 0 is available at:
http://www.public.asu.edu/~ltang9/Lisp/AI-P0.lisp

You might want to check it out.  Keep in mind that the solution is just one version. There could be other ways of implementation.

Besides, I'd like to introduce some useful macros you might be interested:
1). CLISP doesn't provide "while" like C or Java. But actually you can define it by yourself as follows:

(defmacro while (condition &rest forms)
           `(loop (unless ,condition (return)) ,@forms))


A sample call would be:
CL-USER> (let ((x 3))
              (while (> x 0)
                 (format t "~d~%" x)
                 (decf x)))
3
2
1
NIL
Please include the macro definition in your source file if you use "whlie" in the follow-up project.

2). How to interupt an infinite loop in Lisp?
Sometimes, your lisp code might run forever. If you are using lisp-in-a-box, just press "Ctrl-C Ctrl-C" and then the interpreter will enter the bebugging mode. You can just type "q" to terminate the evaluation.

3). Some useful commands to debug in lisp.  You can use "trace" in lisp to track a function.
Also, you can use the following macro to display the variable values at any stage.


(defmacro display (&rest symbols)
  (let ((format-statements
          (loop
             for sym in symbols
             collect `(format t  "~a: ~s~%" ',sym ,sym))))
    `(progn ,@format-statements)))


A sample call is below:
CL-USER> (let ((a 2) (b 4))
  (let ((c (+ a b)))
    (display a b c *package* MOST-POSITIVE-FIXNUM)
    c))
A: 2
B: 4
C: 6
*PACKAGE*: #<PACKAGE COMMON-LISP-USER>
MOST-POSITIVE-FIXNUM: 281474976710655

6

Have fun! :-)
-Lei