Racket 定义:应为变量,但找到零件
我从下面网格中的函数邻居那里得到定义错误,我需要使用“let”从定义中声明吗Racket 定义:应为变量,但找到零件,racket,Racket,我从下面网格中的函数邻居那里得到定义错误,我需要使用“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 pick
;; 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))]
))
;;目的:制作一个迷宫,是一个整数(单元格)列表
;; 介于0和网格大小的平方之间(网格大小^2)
;; 显示迷宫的列表将从0开始并展开
;; 通过随机挑选一个已经在迷宫中的细胞
;; 然后随机选择相邻单元(水平和垂直轴
;仅适用于)以下测试,以确保新的相邻电池:
; a) 在网格/迷宫区域内
; b) 还没有进入迷宫
; c) 与迷宫中已存在的单元格不相邻(otherwide可能会阻塞迷宫路径)
; d) 可能会在这里添加测试,以确保迷宫不会自行消耗
; 网格布局如下(假设网格大小为3;
; 0 1 2
; 3 4 5
; 6 7 8
(定义网格大小15)
(定义a迷宫(列表0))
;返回值是现有a-maze的一部分
(检查expect(成员?(随机单元a-迷宫)a-迷宫)
#(对)
;返回的vale是一个数字吗?
(检查期望值(数字)(随机单元a-maze))
#(对)
;返回的值在网格的限制范围内
(选中expect(列表中的随机数
(定义(随机单元格a列表)
(列表参考a列表(随机(长度a列表)))
(选中expect(成员?15(邻居(随机单元a-迷宫))#true)
(选中expect(成员1(邻居(随机单元a迷宫))#true)
(选中expect(成员?-1(邻居(随机单元a-迷宫))#true)
(选中expect(成员?-15(邻居(随机单元a-迷宫))#true)
(检查expect(((长度(网格中的邻居(随机单元a-迷宫))2)#true)
;邻居:表示单元格的非空整数->列表
;相邻单元格-仅使用水平轴和垂直轴(无对角线)
(定义(相邻成员单元)
(列表(+成员单元网格大小);下面的单元
(+成员单元格1);单元格直接右侧
(-成员单元格1);单元格紧邻左侧
(-成员单元网格大小);上面的单元
)
)
;网格中的邻居:潜在邻居的非空列表->网格中已验证邻居的缩小列表
(定义(网格中的邻居(邻居(随机单元a-迷宫)))
(cond[(<0(第一邻域)>网格大小)(删除(第一邻域))]
[(<0(第二邻域)>网格大小)(删除(第二邻域))]
[(<0(第三邻域)>网格大小)(删除(第三邻域))]
[(<0(第四邻域)>网格大小)(删除(第四邻域))]
))
这里的问题与网格中的邻居函数的第一行有关
具体而言,使用以下形状定义函数:
(define (<name-of-function> <name-of-argument> ...)
<body-of-function>)
这不符合模式。参数的名称是什么?根据此行后面的代码,我认为您需要一个名为的参数。如果是,则此代码应改为:
(define (neighbours-in-grid neighbours)
(cond ...))
我认为这里的混乱部分可能源于这样一个事实,即分别有一个名为neights的函数。这里的问题与网格中neights
函数的第一行有关
具体而言,使用以下形状定义函数:
(define (<name-of-function> <name-of-argument> ...)
<body-of-function>)
这不符合模式。参数的名称是什么?根据此行后面的代码,我认为您需要一个名为的参数。如果是,则此代码应改为:
(define (neighbours-in-grid neighbours)
(cond ...))
我认为这里的混乱部分可能源于这样一个事实,即单独使用一个名为neights的函数。我正在尝试将函数neights的结果放入gridYep中的函数heighbours中,这很好。这意味着,当您在grid函数中调用neights时,您需要使用neights函数。因此例如,您的check expect可能包含(网格中的邻居(邻居(随机单元a-maze))
.DrRacket的“stepper”工具在这里可能对您有用。我正在尝试将函数Neights的结果放入gridYep中的函数heighbours中,这很好。这意味着,当您在grid函数中调用Neights时,您将希望使用Neights函数。例如,您的check expect可能包含(网格中的邻居(邻居(随机单元a-迷宫))
。DrRacket的“步进”工具可能对您有用。