首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >定义:期望变量,但找到一个部分

定义:期望变量,但找到一个部分
EN

Stack Overflow用户
提问于 2016-03-19 18:56:39
回答 1查看 2.4K关注 0票数 1

我正在从下面的函数邻居获得定义错误,我需要使用"let“从定义内部声明吗?

代码语言:javascript
复制
;; Purpose: Produce a maze, being a list of whole numbers (cells) 
;; between 0 and the square of grid-size (grid-size^2)
;; The list prepresenting the maze will start with 0 and be expanded 
;; by randomly picking a cell already in the maze
;;  then randomly selecting a neighbouring cell (horz and vert axis 
;; only) following tests to ensure that the new neighbouring cell is:
;   a) within the grid/maze area
;   b) not already in the maze
;   c) not adjacent to a cell already in the maze (otherwide could block the maze path)
;   d) will likely add tests here to make sure maze does not consume itself
; Grid will lay out as follows (assuming grid-size is 3;
;   0  1  2
;   3  4  5
;   6  7  8

(define grid-size 15)
(define a-maze (list 0))

;Is returned value part of the existing a-maze
(check-expect (member? (random-cell a-maze) a-maze)
          #true)
;Is returned vale a number?
(check-expect (number? (random-cell a-maze))
          #true)

;Is returned value within the limits of the grid
(check-expect (<= 0 (random-cell a-maze) (sqr grid-size))
          #true)

;random-cell: non-empty list -> random number from list
(define (random-cell a-list)
   (list-ref a-list (random (length a-list))))


(check-expect (member? 15 (neighbours (random-cell a-maze))) #true)
(check-expect (member? 1 (neighbours (random-cell a-maze))) #true)
(check-expect (member? -1 (neighbours (random-cell a-maze))) #true)
(check-expect (member? -15 (neighbours (random-cell a-maze))) #true)



(check-expect (= (length (neighbours-in-grid (random-cell a-maze))) 2) #true)

;neighbours: non-empty whole number representing a cell -> list of     
;neighbouring cells - use horz and vert axis only (no diagonals)
(define (neighbours member-cell)
  (list (+ member-cell grid-size) ;cell below
        (+ member-cell 1) ;cell immediate right
        (- member-cell 1) ;cell immediate left
        (- member-cell grid-size) ;cell above
      )

   )

  ;neighbours-in-grid: non-empty list of potential neighbours -> narrowed list of validated neighbours that are in the grid
(define (neighbours-in-grid (neighbours (random-cell a-maze)))
  (cond [(< 0 (first neighbours) > grid-size) (remove (first neighbours))]
        [(< 0 (second neighbours) > grid-size) (remove (second neighbours))]
        [(< 0 (third neighbours) > grid-size) (remove (third neighbours))]
        [(< 0 (fourth neighbours) > grid-size) (remove (fourth neighbours))]
       ))
EN

回答 1

Stack Overflow用户

发布于 2016-03-19 19:43:56

这里的问题与neighbours-in-grid函数的第一行有关。

具体来说,使用此形状定义了一个函数:

代码语言:javascript
复制
(define (<name-of-function> <name-of-argument> ...)
   <body-of-function>)

函数的第一行如下所示:

代码语言:javascript
复制
(define (neighbours-in-grid (neighbours (random-cell a-maze)))
  (cond ...))

这不符合模式。这些争论的名字是什么?基于这行后面的代码,在我看来,您需要一个名为neighbours的参数。如果是这样的话,这段代码应该改为:

代码语言:javascript
复制
(define (neighbours-in-grid neighbours)
   (cond ...))

我认为,这里的部分混乱可能源于这样一个事实,即单独地,您有一个名为“邻居”的函数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36105808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档