I'm working on an implementation of knights tour using DFS in Racket.
I looked up how to get define to work inside cond, and saw that wrapping define in let would make it work. But now I get this error:
begin
(possibly implicit): no expression after a sequence of internal definitions in:(begin (define nxn (cons n n)) (define array (for/vector ((x (car nxn))) (for/vector ((y (cdr nxn))) 0))))
Any idea why this is?
A define
outside of the top-level is called an internal definition. Internal definitions do not create top/module-level bindings; instead, they're syntactic sugar for an equivalent letrec*
expression. That means that the bindings created by the internal definitions are visible only within the lambda/scope where the internal definitions live; in your case, that means they are visible only within the (let () ...)
.
In particular, this means that you cannot conditionally define variables the way you were trying to do in your cond
. Remember that (since you're already using an imperative programming style) you can just create top-level bindings set initially to #f
(or some other blank value, though #f
is customary), and then conditionally set!
them to the values you want.
Beyond that, there are many other things wrong with your code. For example:
eq?
, use =
.else
; that's invalid.else
branch, you had a number of define
forms that are invalid. Remember you cannot conditionally define new variables.genSuccessors
has way too many cond
expressions. You want to have one single cond
expression with many branches within it. Also you need to break up your branch conditions into multiple lines for easier reading.