From: Mike Chu
Subject: Please help me out!!
Date: 
Message-ID: <36C531AB.9A2EBD7F@hotmail.com>
excuse me, I am currently working on the N*N crossword puzzles, but I
got stuck when I try to write an OK function which checks that  whether
this transpose satisfies the conditions.  I just don't know how to
compare the item from transpose to each item in the dictionary ("aaa"
"bbb" "aba") in the OK function.  So could anyone give me some ideas how
to solve it??  Any advice will be deeply appreciated, and thanks in
advance!!!

(setq word-length 3)
(setq Dict '("aaa" "bbb" "aba"))

(defun search (level solution Dict)
  (do((Rest Dict (cdr Rest)))
     ((null Rest) nil)
     (let ((New-sol (append solution (list (car Rest))))))
     (cond
     ((and (OK (transpose-word New-sol))
           (equal level word-length))
       (print New-sol)))
     ((and (OK (transpose-word New-sol))
           (search (+ level 1) New-sol Dict))
     (t nil))))

(defun OK(L)
   (do((Rest Dict (cdr Rest)))
      ((null Rest) nil)
      (cond
      ((equal (car Rest) (car L) t) (OK(cdr L)))
      (t nil))))
From: Martti Halminen
Subject: Re: Please help me out!!
Date: 
Message-ID: <36C5DA46.14E7@rm_spam_trap.dpe.fi>
Mike Chu wrote:
> 
> excuse me, I am currently working on the N*N crossword puzzles, but I
> got stuck when I try to write an OK function which checks that  whether
> this transpose satisfies the conditions.  I just don't know how to
> compare the item from transpose to each item in the dictionary ("aaa"
> "bbb" "aba") in the OK function.  So could anyone give me some ideas how
> to solve it??  Any advice will be deeply appreciated, and thanks in
> advance!!!

First of all, if you are just doing these on paper, quit immediately.
Lisp programming is far more easily done using an operative Lisp
environment, so you can try stuff and correct your errors as soon as you
notice them.

The code you sent won't compile. You should correct those errors before
trying to proceed further. If your Lisp system didn't complain about
this code, time to get a better one.

Here's what Allegro had to say about your code:

; While compiling SEARCH in C:\TEMP\cda8c645.cl:
Warning: Compiling a FUNCTION definition for the name SEARCH as a
DEFUN.  This name is in the COMMON-LISP package and redefining it will
be a violation for portable programs.  Replacing the current definition
of #<Function SEARCH> may be dangerous.  The package COMMON-LISP has
PACKAGE-DEFINITION-LOCK set, which causes the system to signal this
violation.
Warning: Free reference to undeclared variable NEW-SOL assumed special.
Warning: Free reference to undeclared variable WORD-LENGTH assumed
special.
Error: Function position must contain a symbol or lambda expression:
(AND (OK (TRANSPOSE-WORD NEW-SOL)) (SEARCH (+ LEVEL 1) NEW-SOL DICT))...

As you might guess from this, naming your own functions the same as some
built-in part in the language (search) is usually a bad idea. If you
don't want to play with packages, better change the name.

You might use defvar to introduce your global variables to get rid of
those warnings.

That last error comes from failing to count the parentheses correctly.
You should get an editor which counts those, no self-respecting
programmer should waste time doing it himself.

; While compiling OK in C:\TEMP\cda8c643.cl:
Warning: Free reference to undeclared variable DICT assumed special.
Warning: EQUAL should be given exactly 2 arguments.  It was given 3
arguments.
Problem detected when processing
       (EQUAL (CAR REST) (CAR L) ...)

Yet another misplaced parenthesis.

You should get an editor which can indent your code according to its
structure: most Lisp programmers read it based on the indentation, often
not noticing the parentheses conciously at all.

Most Lisp programmers use some variant of Emacs for this purpose.
Here's how it would indent your code:

(defun search (level solution dict)
  (do((rest dict (cdr rest)))
      ((null rest) nil)
    (let ((new-sol (append solution (list (car rest))))))
    (cond
     ((and (ok (transpose-word new-sol))
           (equal level word-length))
      (print new-sol)))
    ((and (ok (transpose-word new-sol))
	  (search (+ level 1) new-sol dict))
     (T nil))))

;; Your let is doing nothing useful. Too many )

(defun ok (L)
  (do((rest dict (cdr rest)))
      ((null rest) nil)
    (cond
     ((equal (car rest) (car l) T)
      (ok (cdr L)))
     (T nil))))


As a style point, DO is seldom used these days: many people find DOLIST
or LOOP preferable when simply looping through a list; another group
would prefer MAPC for this case.

If you really want to use do, you might learn how to use it. Now your
function OK would return NIL regardless of its input (assuming you
corrected it enough not to crash).

As for your problem with OK:
you are not very specific on what you are giving it to work on. If you
are just trying to check if one string exists in your dictionary, how
about

(defun ok (word)
  (member word dict :test #'equal))

If you needed something more complicated, you might try describing what
it should do in english first.



--