문제

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:

  1. Don't compare numbers using eq?, use =.
  2. Don't use parentheses around the else; that's invalid.
  3. Inside the else branch, you had a number of define forms that are invalid. Remember you cannot conditionally define new variables.
  4. Your 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.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top