From: david
Subject: my functions works (i think), now i am stuck
Date: 
Message-ID: <e36bfa33-3ef3-41c0-8651-a91def59c48a@p20g2000yqi.googlegroups.com>
hello lispers. sorry for all my random posts today.
i have a function legal-position which either returns a legal
chess position or nil. i hope. now i would like to do
something like call my function and if it returns nil
have it run until it return a legal position. please help
me cll, you're my only hope.
thanks in advance, 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 empty-cell-p (cell) (eql 0 cell))

(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 (state board) j i)
                :do (if (empty-cell-p 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 '(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 legal-position()
	   (let* ((lst (get-positions))
		  (wk (rank-file (cdr (assoc 'wk lst))))
		  (bk (rank-file (cdr (assoc 'bk lst)))))
	     	     (if (not (member (reverse-rank-file bk)
				      (mapcar #'reverse-rank-file (bad-squares wk))))
			 lst nil)))

From: david
Subject: Re: my functions works (i think), now i am stuck
Date: 
Message-ID: <09d856dd-dbc6-4c78-95bf-d3e01f7a025c@o36g2000yqh.googlegroups.com>
after i post i thought to do this. it seems to work.
sorry more for random postings. i am excited about
my code. anyway, please criticize and help me improve.
thanks, david

(defun legal-position()
	   (let* ((lst (get-positions))
		  (wk (rank-file (cdr (assoc 'wk lst))))
		  (bk (rank-file (cdr (assoc 'bk lst)))))
	     	     (if (not (member (reverse-rank-file bk)
				      (mapcar #'reverse-rank-file (bad-squares wk))))
			 lst (legal-position))))
From: Pascal J. Bourguignon
Subject: Re: my functions works (i think), now i am stuck
Date: 
Message-ID: <87prh049dj.fsf@galatea.local>
david <······@gmail.com> writes:

> hello lispers. sorry for all my random posts today.
> i have a function legal-position which either returns a legal
> chess position or nil. i hope. now i would like to do
> something like call my function and if it returns nil
> have it run until it return a legal position. please help
> me cll, you're my only hope.
> thanks in advance, david

(loop :until (your-function))

If you wanted to return the last value returned by your-function:

(loop :for result = (your-function) :until result :finally (return result))


Notice that (if (f) (loop :until (f))) is equivalent to (loop :until (f))
(This means that you can implement IF with conditional loops).

-- 
__Pascal Bourguignon__
From: William James
Subject: Re: my functions works (i think), now i am stuck
Date: 
Message-ID: <gogsjt09vm@enews4.newsguy.com>
Pascal J. Bourguignon wrote:

> If you wanted to return the last value returned by your-function:
> 
> (loop :for result = (your-function) :until result :finally (return
> result))

Clojure:

; An example function for testing purposes.
(defn foo [] (let [r (rand)] (if (> r 0.9) r false)))


(take 1 (filter (fn [x] x) (repeatedly foo)))
From: William James
Subject: Re: my functions works (i think), now i am stuck
Date: 
Message-ID: <gogspt0a3v@enews4.newsguy.com>
William James wrote:

> Pascal J. Bourguignon wrote:
> 
> > If you wanted to return the last value returned by your-function:
> > 
> > (loop :for result = (your-function) :until result :finally (return
> > result))
> 
> Clojure:
> 
> ; An example function for testing purposes.
> (defn foo [] (let [r (rand)] (if (> r 0.9) r false)))
> 
> 
> (take 1 (filter (fn [x] x) (repeatedly foo)))

Make that

(first (filter (fn [x] x) (repeatedly foo)))
From: Marco Antoniotti
Subject: Re: my functions works (i think), now i am stuck
Date: 
Message-ID: <fb7442d3-97f5-422c-a150-bb517aacdf9d@w9g2000yqa.googlegroups.com>
On 2 Mar, 16:08, "William James" <> wrote:
> William James wrote:
> > Pascal J. Bourguignon wrote:
>
> > > If you wanted to return the last value returned by your-function:
>
> > > (loop :for result = (your-function) :until result :finally (return
> > > result))
>
> > Clojure:
>
> > ; An example function for testing purposes.
> > (defn foo [] (let [r (rand)] (if (> r 0.9) r false)))
>
> > (take 1 (filter (fn [x] x) (repeatedly foo)))
>
> Make that
>
> (first (filter (fn [x] x) (repeatedly foo)))

(1) It ain't Ruby, (2) it ain't F#.  But it is (almost) Coming
Lisp. :)

Cheers
--
Marco
From: =?UTF-8?B?QW5kcsOpIFRoaWVtZQ==?=
Subject: Re: my functions works (i think), now i am stuck
Date: 
Message-ID: <govc90$1al$1@news.motzarella.org>
William James schrieb:
> William James wrote:
> 
>> Pascal J. Bourguignon wrote:
>>
>>> If you wanted to return the last value returned by your-function:
>>>
>>> (loop :for result = (your-function) :until result :finally (return
>>> result))
>> Clojure:
>>
>> ; An example function for testing purposes.
>> (defn foo [] (let [r (rand)] (if (> r 0.9) r false)))
>>
>>
>> (take 1 (filter (fn [x] x) (repeatedly foo)))
> 
> Make that
> 
> (first (filter (fn [x] x) (repeatedly foo)))

Good good, you already made that correction of replacing (take 1) with
(first).
But I would suggest to use identity:
(first (filter identity (repeateyly foo)))

I don’t care that it saves to chars. Even if it wouldn’t: I find it
clearer to reuse an existing function. If I was after the charcount then
maybe (first (filter #(do %) (repeatedly foo))) would be a good solution.


André
-- 
Lisp is not dead. It’s just the URL that has changed:
http://clojure.org/
From: William James
Subject: Re: my functions works (i think), now i am stuck
Date: 
Message-ID: <gp06p402998@enews2.newsguy.com>
William James wrote:

> William James wrote:
> 
> > Pascal J. Bourguignon wrote:
> > 
> > > If you wanted to return the last value returned by your-function:
> > > 
> > > (loop :for result = (your-function) :until result :finally (return
> > > result))
> > 
> > Clojure:
> > 
> > ; An example function for testing purposes.
> > (defn foo [] (let [r (rand)] (if (> r 0.9) r false)))
> > 
> > 
> > (take 1 (filter (fn [x] x) (repeatedly foo)))
> 
> Make that
> 
> (first (filter (fn [x] x) (repeatedly foo)))

(first (filter identity (repeatedly foo)))