From: david
Subject: please review my code
Date: 
Message-ID: <27ddb5b9-9519-41d0-8101-a17fd9d0bd81@p11g2000yqe.googlegroups.com>
thanks to everyone.

defclass chessboard ()
  ((board :accessor board
	           :initform (make-array '(8 8) :initial-element nil))))

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

(defmethod print-board-to-string ((board chessboard))
  (with-output-to-string (*standard-output*)
    (flet ((columns ()
	     (format t "    | A  | B  | C  | D  | E  | F  | G  | H  |    ~
%"))
           (line    ()
	     (format t "----+----+----+----+----+----+----+----+----+----~
%")))
      (loop :for i :from 7 :downto 0
         :initially (terpri) (columns) (line)
         :do (loop :for j :from 0 :below 8
                :initially (format t " ~2D |" (1+ i))
                :for cell = (aref (board board) j i)
                :do (if (null cell)
                        (princ "    |")
                        (format t " ~2A |" cell))
                :finally (format t " ~2D ~%" (1+ i)) (line))
         :finally (columns)))))

(defmethod print-object ((board chessboard) stream)
  (print-unreadable-object (board stream :identity t :type t)
    (let ((*print-readably* nil))
      (format stream " ~A" (print-board-to-string board))))
  board)

(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 '(K N B |k|) (get-four-unique-random-numbers)))

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

(defun bad-squares (k)
"bad-squares is bad because it doesn't care about edge cases"
  (let ((bad-squares
	 (loop for x in (neighbor (first k))
	    append (loop for y in (neighbor (second k))
		      collect (list x y))))) bad-squares))

(defun legal-position()
  (let* ((lst (get-positions))
	 (k (rank-file (cdr (assoc 'k lst))))
	 (x (rank-file (cdr (assoc '|k| lst)))))
    (if (not (member (reverse-rank-file x)
		     (mapcar #'reverse-rank-file (bad-squares k))))
	lst (legal-position))))

(defun numberify-position ()
           (let ((lst (legal-position)))
             (loop for i in '(k n b |k|)collect
                  (cdr (assoc i lst)))))

(defun random-chessboard ()
"Returns a fresh random chessboard with pieces wk wb wn and b."
   (let ((chessboard (make-instance 'chessboard)))
     (mapc #'(lambda (pos piece)
              (put-piece chessboard (first pos)
                         (second pos) piece))
          (mapcar #'rank-file (numberify-position)) '(k b n |k|))
     chessboard))

(defun encode-row-into-fen (cells)
    (loop
       :with results = '()
       :for cell :in cells
       :do (cond
             (cell                       (push cell results))
             ((integerp (first results)) (incf (first results)))
             (t                          (push 1 results)))
       :finally (return (list (intern (format nil "~{~A~}" (reverse
results)))))))

(defun fen-list-from-board (board)
  (mapcar #'encode-row-into-fen
	  (loop :for j :from 7 :downto 0
                :collect (loop
                           :for i :from 0 :to 7
                           :collect (aref (board board) i j)))))

(defun generate-fen-notation (board)
  (format nil "~{~{~A~}~^/~} b - - 0 1" (fen-list-from-board board)))

(defun write-fen-to-file ()
  (with-open-file (stream "/home/david/bkend.fen"
			  :direction :output
                          :if-exists :supersede
                          :if-does-not-exist :create)
    (write-line (generate-fen-notation (random-chessboard)) stream)))

From: William James
Subject: Re: please review my code
Date: 
Message-ID: <gpjlmg06tv@enews5.newsguy.com>
david wrote:

> thanks to everyone.

Your code stinks.  It's even worse than the garbage churned out
by Pasquale "COBOL" Boorguido.

There are two possible explanations:

1. You're trying to make Commune Lisp look terrible.
2. You're an idiot.
From: david
Subject: Re: please review my code
Date: 
Message-ID: <a47f15a6-7bb4-444f-8f1e-addcf8bd99a4@o36g2000yqh.googlegroups.com>
On Mar 15, 2:42 pm, "William James" <·········@yahoo.com> wrote:
> david wrote:
> > thanks to everyone.
>
> Your code stinks.  It's even worse than the garbage churned out
> by Pasquale "COBOL" Boorguido.
>
> There are two possible explanations:
>
> 1. You're trying to make Commune Lisp look terrible.
> 2. You're an idiot.

thanks for your input. i have written a function just for
you.
CL-USER> (defun print-kitty-just-for-william-james ()
	   (setf a "                _
                \`*-.
                 )  _`-.
                .  : `. .
                : _   '  \
                ; *` _.   `*-._
                `-.-'          `-.
                  ;       `       `.
                  :.       .        \
                  . \  .   :   .-'   .
                  '  `+.;  ;  '      :
                  :  '  |    ;       ;-.
                  ; '   : :`-:     _.`* ;
         [bug] .*' /  .*' ; .*`- +'  `*'
               `*-*   `*-*  `*-*'
"))
From: david
Subject: Re: please review my code
Date: 
Message-ID: <d3b52ed5-735e-4ec7-8637-c901fbf38721@41g2000yqf.googlegroups.com>
On Mar 15, 2:50 pm, david <······@gmail.com> wrote:
> On Mar 15, 2:42 pm, "William James" <·········@yahoo.com> wrote:
>
> > david wrote:
> > > thanks to everyone.
>
> > Your code stinks.  It's even worse than the garbage churned out
> > by Pasquale "COBOL" Boorguido.
>
> > There are two possible explanations:
>
> > 1. You're trying to make Commune Lisp look terrible.
> > 2. You're an idiot.
>
> thanks for your input. i have written a function just for
> you.
> CL-USER> (defun print-kitty-just-for-william-james ()
>            (setf a "                _
>                 \`*-.
>                  )  _`-.
>                 .  : `. .
>                 : _   '  \
>                 ; *` _.   `*-._
>                 `-.-'          `-.
>                   ;       `       `.
>                   :.       .        \
>                   . \  .   :   .-'   .
>                   '  `+.;  ;  '      :
>                   :  '  |    ;       ;-.
>                   ; '   : :`-:     _.`* ;
>          [bug] .*' /  .*' ; .*`- +'  `*'
>                `*-*   `*-*  `*-*'
> "))

floopio:
 f->k!
From: Kenneth Tilton
Subject: Re: please review my code
Date: 
Message-ID: <49bdb529$0$29326$607ed4bc@cv.net>
William James wrote:
> david wrote:
> 
>> thanks to everyone.
> 
> Your code stinks.  It's even worse than the garbage churned out
> by Pasquale "COBOL" Boorguido.

Mr. Sociable impales himself again in c.l.lisp, the Home For Unlikeable 
Geeks. Sadly we had our endowment invested with Maddow and we our short 
a bed and froggy has tenure...


kt
From: Thomas A. Russ
Subject: Re: please review my code
Date: 
Message-ID: <ymik56poxvm.fsf@blackcat.isi.edu>
Some simplifications:

david <······@gmail.com> writes:

IN 
> (defmethod print-board-to-string ((board chessboard))
...
>                 :do (if (null cell)
>                         (princ "    |")
>                         (format t " ~2A |" cell))

...)



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

Replace with:
 
 (defun rank-file (position)
    (multiple-value-list (truncate position 8)))


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

Generally, if there is no repeated use, I would just inline the
accessors, and also use them in order:

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

But to make this even more lisp-like, you could do something using
simple macros to turn the list into something more like a data
structure:

(defmacro x-position (list)
  `(first ,list))
(defmacro y-position (list)
  `(second ,list))
(defmacro make-position (x y)
  `(list ,x ,y))

Then you could write:

  (defun reverse-rank-file (lst)
     (+ (* 8 (x-position lst)) (y-position lst)))


-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: david
Subject: Re: please review my code
Date: 
Message-ID: <e430ee4d-7dc8-4e67-a9fb-a54fbe477c33@o36g2000yqh.googlegroups.com>
On Mar 16, 6:23 pm, ····@sevak.isi.edu (Thomas A. Russ) wrote:
> Some simplifications:
> > (defun reverse-rank-file (lst)
> >   (let ((x (first lst))
> >    (y (second lst)))
> >     (+ y (* 8 x))))
>
> Generally, if there is no repeated use, I would just inline the
> accessors, and also use them in order:
>
>    (defun reverse-rank-file (lst)
>       (+ (* 8 (first lst)) (second lst)))

i think i was using a let for everything that day :)
thanks for all your help.
-david
From: Daniel Janus
Subject: Re: please review my code
Date: 
Message-ID: <slrngrur51.52a.przesunmalpe@students.mimuw.edu.pl>
On 16.03.2009 Thomas A. Russ <···@sevak.isi.edu> wrote:

> But to make this even more lisp-like, you could do something using
> simple macros to turn the list into something more like a data
> structure:
>
> (defmacro x-position (list)
>   `(first ,list))
> (defmacro y-position (list)
>   `(second ,list))
> (defmacro make-position (x y)
>   `(list ,x ,y))

Isn't the first macro-writing rule to avoid writing macros where a simple 
function would suffice?

-- 
Daniel 'Nathell' Janus, ······@nathell.korpus.pl, http://korpus.pl/~nathell
- Pro� m� �lov�k ml�et?
- Aby sly�el melodie lid� kolem sebe.
   [Rok diab�a]
From: Pascal J. Bourguignon
Subject: Re: please review my code
Date: 
Message-ID: <7ctz5s8oqj.fsf@pbourguignon.anevia.com>
Daniel Janus <············@nathell.korpus.pl> writes:

> On 16.03.2009 Thomas A. Russ <···@sevak.isi.edu> wrote:
>
>> But to make this even more lisp-like, you could do something using
>> simple macros to turn the list into something more like a data
>> structure:
>>
>> (defmacro x-position (list)
>>   `(first ,list))
>> (defmacro y-position (list)
>>   `(second ,list))
>> (defmacro make-position (x y)
>>   `(list ,x ,y))
>
> Isn't the first macro-writing rule to avoid writing macros where a simple 
> function would suffice?

Indeed.  But actually the problem is that newbies are not lazy enough.

C/USER[22]> (defstruct (cro (:type list)
            (:conc-name nil)
            (:constructor make-position (x-position y-position)))
    x-position y-position)
CRO
C/USER[23]>  (make-position 42 24)
(42 24)
C/USER[24]> (x-position (make-position 42 24))
42
C/USER[25]> (y-position (make-position 42 24))
24
C/USER[26]> 

-- 
__Pascal Bourguignon__
From: Thomas A. Russ
Subject: Re: please review my code
Date: 
Message-ID: <ymid4cgow4b.fsf@blackcat.isi.edu>
Daniel Janus <············@nathell.korpus.pl> writes:

> On 16.03.2009 Thomas A. Russ <···@sevak.isi.edu> wrote:
> 
> > But to make this even more lisp-like, you could do something using
> > simple macros to turn the list into something more like a data
> > structure:
> >
> > (defmacro x-position (list)
> >   `(first ,list))
> > (defmacro y-position (list)
> >   `(second ,list))
> > (defmacro make-position (x y)
> >   `(list ,x ,y))
> 
> Isn't the first macro-writing rule to avoid writing macros where a simple 
> function would suffice?

Generally.  But by using a macro for the position accessors, you get the
SETF expansions for free.

I agree that MAKE-POSITION should be a function, so that you can APPLY
it.

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