3 Replies - 3556 Views - Last Post: 15 June 2011 - 09:43 PM

#1 I X Code X 1   User is offline

  • D.I.C Head
  • member icon

Reputation: 47
  • View blog
  • Posts: 66
  • Joined: 28-July 10

Stack Overflow Problem - Common Lisp

Posted 27 May 2011 - 11:01 AM

Hi, I am making a common lisp tic-tac-toe game and am having a problem with my function to determine if it is a cats game or not:

 (defun cats? ()
	(let ((x 0))
	    (if (eq (aref *board* x) 'nil)
		'not-a-cats
		(progn (unless (equal x 8)
		    (progn (1+ x)
			(cats?))))))) 


What I want this function to do is to set a local variable, x, to be 0. Then it checks if the spot x (in the first run though it would be 0) is equal to nil. If this spot is equal to nil I know it is not a cats game. I believe the problem is coming in the next part. I'm trying to say as long as x does not equal 8 add one to x and call cats again. This way it will look in every spot of the array for a nil. If no nil is found, it's a cats game.


I'm getting this error:

[172]> (cats?)

*** - Program stack overflow. RESET

Any tips? Also, if there is any better way of checking if every value in the array is nil or not I would love to know!

Is This A Good Question/Topic? 0
  • +

Replies To: Stack Overflow Problem - Common Lisp

#2 I X Code X 1   User is offline

  • D.I.C Head
  • member icon

Reputation: 47
  • View blog
  • Posts: 66
  • Joined: 28-July 10

Re: Stack Overflow Problem - Common Lisp

Posted 27 May 2011 - 11:18 AM

I also have tried it with the find function.

 (defun cats? ()
	(if (not (find 'nil *board*))
		'cat
		'not-cat)) 


Still not working how I want. Is returning true every time? :helpsmilie:

This post has been edited by I X Code X 1: 27 May 2011 - 11:21 AM

Was This Post Helpful? 0
  • +
  • -

#3 I X Code X 1   User is offline

  • D.I.C Head
  • member icon

Reputation: 47
  • View blog
  • Posts: 66
  • Joined: 28-July 10

Re: Stack Overflow Problem - Common Lisp

Posted 27 May 2011 - 11:33 AM

Fixed it -

I think the problem had to do with my checking for nil. Nil was causing things to happen opposite of how I expected and couldn't really find a way around it. So, what I did was make the initial contents of my board array to be something else.

(defparameter *board* (make-array 9 :initial-contents '(1 1 1 1 1 1 1 1 1)))

      (defun cats? ()
	    (if (not (find 1 *board*))
		t
		'nil)) 


That made for a lot less confusion and work.

This post has been edited by I X Code X 1: 27 May 2011 - 11:34 AM

Was This Post Helpful? 0
  • +
  • -

#4 I X Code X 1   User is offline

  • D.I.C Head
  • member icon

Reputation: 47
  • View blog
  • Posts: 66
  • Joined: 28-July 10

Re: Stack Overflow Problem - Common Lisp

Posted 15 June 2011 - 09:43 PM

Just reading back over this post:

I wanted to check each element in the list and I would know it's not a "cat's game" if there was at least one element of the list that was nil (not going to explain exactly how I was implementing this game at the moment). So I wrote my own function to do this, but the answer was actually much more trivial than that:

Common Lisp hosts several built in functions that can search a list (or a vector, array...most sequences) and decide if every element in the list is nil (or anything for that matter) or if some of the elements are nil.

So, for this function I could have very easily checked to see if there was at least one element in the list that was nil with this:

(some #'null '(1 2 3 nil nil 4 5))
; this will return T because [b]"some"[/b] of the elements are, in fact, ;nil.


; every works exactly the same way except, as the name suggests, it checks ;if every element is 'nil' and returns T if that is the case 



This is also a prime example of a Higher-Order Function at work! :balloon:

This post has been edited by I X Code X 1: 15 June 2011 - 09:46 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1