Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

help with function please

7 views
Skip to first unread message

david

unread,
Mar 1, 2009, 3:53:08 PM3/1/09
to
i attempt to rewrite function illegalp as below. is not working.
please help.
thanks, david

(defclass chessboard ()
((board-position :accessor state
:initform (make-array '(8 8) :initial-element '0))))

(defmethod put-piece ((board chessboard) x y p)
(setf (aref (state board) x y) p))


(defun get-four-unique-random-numbers ()
(loop
:with results = '()
:for alea = (random 64)
:while (< (length results) 4)
:do (pushnew alea results)
:finally (return results)))


(defun rank-file (position)
(multiple-value-bind (quotient remainder)
(truncate position 8)
(list quotient remainder)))

(defun reverse-rank-file (lst)
(let ((x (first lst))
(y (second lst)))
(+ y (* 8 x))))

(defun get-positions ()
(mapcar #'cons '(wk wn wb bk) (get-four-unique-random-numbers)))

(defun neighbor (x)
(list (1- x) x (1+ x)))

(defun bad-squares (wk)
(let ((bad-squares (loop for x in (neighbor (first wk))
append (loop for y in (neighbor (second wk))
collect (list x y)))))
bad-squares))

(defun illegalp (wk bk)
(member (reverse-rank-file bk)
(mapcar #'reverse-rank-file (bad-squares wk))))

(defun new-ill ()
(let* ((lst (get-positions))
(wk (cdr (assoc 'wk lst)))
(bk (cdr (assoc 'bk lst))))
(member (reverse-rank-file bk)
(mapcar #'reverse-rank-file (bad-squares wk)))))


david

unread,
Mar 1, 2009, 4:03:28 PM3/1/09
to
also, function bad-squares does not care if piece is on the
edge of the board. i thought it does not matter so much.
what would lisp experts think about this?
thanks, david

david

unread,
Mar 1, 2009, 4:06:29 PM3/1/09
to
also i think i see problem.
two different types of wk and bk.
but i have to go to work.
later lispers.

Thomas A. Russ

unread,
Mar 2, 2009, 1:53:11 PM3/2/09
to
david <not...@gmail.com> writes:

I would tend to make the function correct, instead of ignoring the
off-board positions.

For you current application, this doesn't matter, but by taking the time
to do a well-formed function definition now, you save the need to go
back and fix bugs if you suddenly decide that you can use this same
function to, for example, show where the king can legally move.

So, part of good software engineering and design is to try to think
beyond the immediate problem and make modular pieces (functions) that
encapsulate some domain-relevant and more general function. That allows
your code to be extensible and reusable without the need to do a lot of
re-writing (not to mention forgetting about those shortcuts 3 months
from now when you try to make changes.)

At the very least, make sure you document any of these assumptions or
shortcuts that you take in the code comments.


--
Thomas A. Russ, USC/Information Sciences Institute

Thomas A. Russ

unread,
Mar 2, 2009, 1:49:54 PM3/2/09
to
david <not...@gmail.com> writes:

> i attempt to rewrite function illegalp as below. is not working.
> please help.

> (defun illegalp (wk bk)


> (member (reverse-rank-file bk)
> (mapcar #'reverse-rank-file (bad-squares wk))))

You need to specify the :TEST argument to MEMBER.

You are testing for list equality, and the default test of EQL will not
do what you want. (Hmmm. I didn't notice that in my other reply).

Try

(defun illegalp (wk bk)
(member (reverse-rank-file bk)
(mapcar #'reverse-rank-file (bad-squares wk))

:test #'equal))

or more simply:

(defun illegalp (wk bk)
(member (rank-file bk) (bad-squares wk) :test #'equal))

William James

unread,
Mar 3, 2009, 2:06:44 AM3/3/09
to
david wrote:

>
> (defun get-four-unique-random-numbers ()
> (loop
> :with results = '()
> :for alea = (random 64)
> :while (< (length results) 4)
> :do (pushnew alea results)
> :finally (return results)))

Insufficient bloat. Make it

(defun
find-and-return-four-unique-random-numbers-between-zero-and-sixty-four

William James

unread,
Mar 3, 2009, 2:30:19 AM3/3/09
to
William James wrote:

find-and-return-four-unique-random-numbers-between-zero-and-sixty-four-
inclusive-and-exclusive-respectively

Kojak

unread,
Mar 3, 2009, 2:41:57 AM3/3/09
to
Le 3 Mar 2009 07:30:19 GMT,
William James a écrit :

> find-and-return-four-unique-random-numbers-between-zero-and-sixty-four-
> inclusive-and-exclusive-respectively

Be nice with your computer...

find-and-return-four-unique-random-numbers-between-zero-and-
sixty-four-inclusive-and-exclusive-respectively-PLEASE

--
Jacques.

Kenneth Tilton

unread,
Mar 3, 2009, 3:07:55 AM3/3/09
to

where's your damn homework, punk? are you saying Ruby cannot scale to a
25-word spec?

This must be terribly embarrassing for you. Look on the bright side:
imagine if I had posted something non-trivial!

Marco Antoniotti

unread,
Mar 3, 2009, 4:14:19 AM3/3/09
to

The Ruby Guy has not discovered Intercal yet. Hence his
impoliteness :)

Cheers
--
Marco

david

unread,
Mar 3, 2009, 3:41:04 PM3/3/09
to
On Mar 3, 1:30 am, "William James" <w_a_x_...@yahoo.com> wrote:
> William James wrote:
> find-and-return-four-unique-random-numbers-between-zero-and-sixty-
four-
> inclusive-and-exclusive-respectively

when i program in common lisp i feel like i'm skiing down
a frozen mountain, the icy wind blowing in my face, as i eat
a peppermint patty sandwiched between two more peppermint patties.
can ruby make me feel like that?

thanks, david

0 new messages