[Issue Solved]Practical Common LISP Chapter 9: Gensyms Issue

Discussion of Common Lisp
Post Reply
HeavenOfSevens
Posts: 2
Joined: Wed Apr 13, 2011 4:58 am

[Issue Solved]Practical Common LISP Chapter 9: Gensyms Issue

Post by HeavenOfSevens » Wed Apr 13, 2011 6:46 am

I wasn't able to find anything similar to my question through the search, so sorry if this has come up before.

I'm a complete beginner to LISP, and have been following Practical Common LISP to learn it, using the recommended LISPBox with Allegro. There weren't any issues until I get to the part in Chapter 9 where the macro (combine-results) is written. My question relates specifically to the use of getsyms. This is the code I end up with following the book:

Code: Select all

(defun test-+ ()
  (check
    (= (+ 1 2) 3)
    (= (+ 1 2 3) 6)
    (= (+ -1 -3) -4)))
(defun report-result (result form)
  (format t "~:[FAIL~;pass~] ... ~a~%" result form)
  result)
(defmacro check (&body forms)
  `(combine-results
     ,@(loop for f in forms collect `(report-result ,f ',f))))
(defmacro combine-results (&body forms)
  (with-gensyms (result)
    `(let ((,result t))
       ,@(loop for f in forms collect `(unless ,f (setf ,result nil)))
       ,result)))
When I compile it, I get the following warnings, which turn into errors when I run (test-+):

Code: Select all

Warning: While compiling these undefined functions were referenced:
         WITH-GENSYMS from position 265 in test.lisp;298
         RESULT from position 265 in test.lisp;298
I can't find any differences between my code and that of the book, so I did some more research into gensyms, and saw the following syntax used:

Code: Select all

(let ((variable (gensym)))
...)
I tried this syntax in my code to come up with this for the (combine-results) macro:

Code: Select all

(defmacro combine-results (&body forms)
  (let ((result (gensym)))
    `(let ((,result t))
       ,@(loop for f in forms collect `(unless ,f (setf ,result nil)))
       ,result)))
Which, for some reason, wasn't working before but has started working as soon as I input it to get some warnings to post here.

So, I suppose my question now is to do with (with-gensyms). Is it actually a function in LISP? And, if so, what is wrong with my use of it?

Thank you in advance.
Last edited by HeavenOfSevens on Wed Apr 13, 2011 1:48 pm, edited 1 time in total.

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Practical Common LISP Chapter 9: Gensyms Issue

Post by ramarren » Wed Apr 13, 2011 8:32 am

A somewhat unrelated note: the name of the language, and for that matter it is spelled like that on the cover of the book, is "Common Lisp", with non-capital letters after the first. It is not really important, but LISP just looks archaic.

Code in PCL is not supposed to be isolated by chapter. WITH-GENSYMS is defined in chapter 8. Also macros should be defined before functions that use them.

HeavenOfSevens
Posts: 2
Joined: Wed Apr 13, 2011 4:58 am

Re: Practical Common LISP Chapter 9: Gensyms Issue

Post by HeavenOfSevens » Wed Apr 13, 2011 12:33 pm

Thanks for picking me up on the spelling of Lisp, I'll do it like that in the future.

Sorry I wasted your time on the WITH-GENSYMS definition in Chapter 8 - I must have just read that section too quickly...

Thanks again for your help.

Post Reply