Grand Theft Wumpus Code Style Question

87 views
Skip to first unread message

Purity Control

unread,
Jan 28, 2011, 7:06:23 AM1/28/11
to Land of Lisp
My compiler is throwing up a style error when compiling the make-city-
edges function (listed on page 139).

It is complaining that the x variable in the lambda function is never
being called.
I can see why the x variable is not being used (we are just randomly
deciding which items should stay in the list and which should not)
What I was wondering was whether there was a "cleaner" way to write
the function so that no style errors were brought up by the compiler

code:
(defun make-city-edges ()
(let* ((nodes (loop for i from 1 to *node-num*
collect i))
(edge-list (connect-all-islands nodes (make-edge-list)))
(cops (remove-if-not (lambda (x)
(zerop (random *cop-odds*)))
edge-list)))
(add-cops (edges-to-alist edge-list) cops )))

Phil Rand

unread,
Jan 28, 2011, 1:09:32 PM1/28/11
to land-o...@googlegroups.com
On Fri, Jan 28, 2011 at 4:06 AM, Purity Control <cr...@craigferry.net> wrote:
My compiler is throwing up a style error when compiling the make-city-
edges function (listed on page 139).

It is complaining that the x variable in the lambda function is never
being called.
I can see why the x variable is not being used (we are just randomly
deciding which items should stay in the list and which should not)
What I was wondering was whether there was a "cleaner" way to write
the function so that no style errors were brought up by the compiler

code:
(defun make-city-edges ()
 (let* ((nodes (loop for i from 1 to *node-num*
                    collect i))
        (edge-list (connect-all-islands nodes (make-edge-list)))
        (cops (remove-if-not (lambda (x)

You might try inserting an ignore declaration here:

  (declare (ignore x))

That tells lisp not to worry if you never use x, and in fact, to
warn you if you do refer to x.  (I haven't tried this myself.)
 
                               (zerop (random *cop-odds*)))
                             edge-list)))
   (add-cops (edges-to-alist edge-list) cops )))



--
Phil Rand
phil...@gmail.com
phil...@pobox.com

Purity Control

unread,
Jan 28, 2011, 1:50:58 PM1/28/11
to Land of Lisp
Thank you very much,

Have just tried this and it works a treat.
It elegantly clarifies the functions intention as well.

Craig
> philr...@gmail.com
> philr...@pobox.com

Christopher Allen-Poole

unread,
Jan 28, 2011, 4:59:07 PM1/28/11
to Land of Lisp
Have you tried getting rid of the "x" in this line:
(cops (remove-if-not (lambda (x)

If your compiler has difficulty with that, you could always add this
right after:
(cons x ()) ; Creates a cons cell which uses that value.

Purity Control

unread,
Jan 30, 2011, 7:43:31 PM1/30/11
to Land of Lisp
The code compiles but results in a runtime crash - presumably because
the remove-if-not function calls the lambda with a single argument but
the lambda declared doesn't take one.

Adding the (cons x()) sorts this out as you suggested.

I think I prefer the (declare (ignore x)) declaration though as this
just makes the code intent that bit clearer - to me anyway.

Many Thanks




On Jan 28, 9:59 pm, Christopher Allen-Poole <christoph...@allen-
Reply all
Reply to author
Forward
0 new messages