我正在从下面的函数邻居获得定义错误,我需要使用"let“从定义内部声明吗?
;; 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))]
))发布于 2016-03-19 19:43:56
这里的问题与neighbours-in-grid函数的第一行有关。
具体来说,使用此形状定义了一个函数:
(define (<name-of-function> <name-of-argument> ...)
<body-of-function>)函数的第一行如下所示:
(define (neighbours-in-grid (neighbours (random-cell a-maze)))
(cond ...))这不符合模式。这些争论的名字是什么?基于这行后面的代码,在我看来,您需要一个名为neighbours的参数。如果是这样的话,这段代码应该改为:
(define (neighbours-in-grid neighbours)
(cond ...))我认为,这里的部分混乱可能源于这样一个事实,即单独地,您有一个名为“邻居”的函数。
https://stackoverflow.com/questions/36105808
复制相似问题