1

I'm trying to created a player that would solve the game Mastermind in LISP. I tried using the following nested loop in a helper function

(defparameter *counter* 0) 

;analyze the score to determine whether points need to be added to counter
(defun analyze (last-response)
  (if (> (first last-response) 0)
      (setf *counter* (+ (first last-response) *counter*))))

;helper function for finding correct color
(defun guessColor (length colors last-response)
    (loop while (< *counter* 4) do
       (loop for i from 1 to length
          collect (first colors)
          (setf colors (rest colors)))
    (analyze (last-reponse))))

;baseline Holyguacamole player will guess all colors before trying all combinations of correct color 
(defun HolyGuacamole (board colors SCSA last-response)
  (declare (ignore SCSA))
  ;(print last-response)
  (guessColor board colors last-response)
  (print *counter*)
    ;(sortColor)
)

The while loop is supposed to run while the global variable *counter* is less than 4. The inside loop is supposed to guess colors based on the length of pegs required (variable length). I've been running into the compilation error

during macroexpansion of (LOOP WHILE (< COUNTER 4) ...). Use ;
BREAK-ON-SIGNALS to intercept.

I'm unfamiliar with LISP so I'm not sure what that error code means and how to fix it. I felt like I nested it correctly with the proper parenthesis, but I'm not actually sure what's wrong with it.

Link to Mastermind environment.

6
  • How large is the whole program? Can you paste it or maybe pare it down to a self-contained example? I don't see a definition of score or analyze or the declaration of *counter* Commented Nov 30, 2016 at 4:10
  • @GregoryNisbet the code for the game itself is very large. I edited to have my helper functions as well as the declaration of counter. score is changed to last-response Commented Nov 30, 2016 at 4:50
  • I think (analyze (last-reponse)))) should be (analyze last-reponse))) ... There also seem to be some other errors... I haven't been able to reproduce that exact compiler error with clisp or sbcl. Commented Nov 30, 2016 at 5:16
  • I think even when I had the parenthesis correct for that, my biggest issue is the loops itself. Commented Nov 30, 2016 at 5:18
  • 2
    The error is probably caused by the (SETF...) in the inner loop. Only one form is allowed in a COLLECT-clause. You probably should put it in a DO-clause. Or you could use POP insted of FIRST and remove the SETF. I'm not sure what you expect the inner loop to achieve though. It collects the first LENGTH elements from COLORS, but its return value is just discarded by the outer loop. Commented Nov 30, 2016 at 5:24

1 Answer 1

1

There is no obstacle in principle to nesting a loop inside another loop. However, a COLLECT or COLLECTING clause can only take a single expression, as @jkiiski pointed out.

For instance, the following program

(defun nested-loop ()
  (loop for i from 1 to 10 doing
        (loop for j from 1 to 10 collecting
              (print "some string")
              (print (list 'nested-loop i j)))))

(nested-loop)

produces a syntax error under CLISP.

*** - LOOP: illegal syntax near (PRINT (LIST 'NESTED-LOOP I J)) in
       (LOOP FOR J FROM 1 TO 10 COLLECTING (PRINT "some string")
        (PRINT (LIST 'NESTED-LOOP I J)))

Using a do or doing clause works, as does grouping the multiple expressions associated with a collecting clause with progn.

(defun nested-loop ()
  (loop for i from 1 to 10 doing
        (loop for j from 1 to 10 collecting
              (progn
                (print "some string")
                (print (list 'nested-loop i j))))))

(nested-loop)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your response. Upon further thought, I don't think using nested loop would work the way I want for this game. I've rewritten my code below. It compiles but when testing with print last-response, it's printing all NIL's

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.