Functional programming 球拍-从列表中随机选取一个元素

Functional programming 球拍-从列表中随机选取一个元素,functional-programming,racket,Functional Programming,Racket,我不知道如何让这个代码工作,现在已经很晚了,我想我的大脑已经停止了工作,有人愿意帮我一下吗 到目前为止的代码: (define maze-size 15) (define-struct Cell (x y)) ; adjacents : Cell -> list-of-Cells ; Produce a list of the four Cells above, below, left, and right of 'cell'. (define(adjacents cell x y)

我不知道如何让这个代码工作,现在已经很晚了,我想我的大脑已经停止了工作,有人愿意帮我一下吗

到目前为止的代码:

(define maze-size 15)
(define-struct Cell (x y))

; adjacents : Cell -> list-of-Cells
; Produce a list of the four Cells above, below, left, and right of 'cell'.

(define(adjacents cell x y)
(list
(make-Cell x (+ y 1))
(make-Cell x (- y 1))
(make-Cell (- x 1) y)
(make-Cell (+ x 1) y)))
(check-expect (member? (random-adjacent (list (make-Cell 123 104)))
                   (list (make-Cell 123 105)
                         (make-Cell 123 103)
                         (make-Cell 122 104)
                         (make-Cell 124 104)))
          #true)
这就是我被难倒的地方,我该如何解决这个问题? 注意:下面的代码不起作用

; random-adjacent : list-of-Cells -> Cell
; Produce a random Cell adjacent to a random Cell from the non-empty list'cells'.

(define (random-adjacent cells)
(random (adjacents cell)))
它的行为应该是这样的:

(define maze-size 15)
(define-struct Cell (x y))

; adjacents : Cell -> list-of-Cells
; Produce a list of the four Cells above, below, left, and right of 'cell'.

(define(adjacents cell x y)
(list
(make-Cell x (+ y 1))
(make-Cell x (- y 1))
(make-Cell (- x 1) y)
(make-Cell (+ x 1) y)))
(check-expect (member? (random-adjacent (list (make-Cell 123 104)))
                   (list (make-Cell 123 105)
                         (make-Cell 123 103)
                         (make-Cell 122 104)
                         (make-Cell 124 104)))
          #true)

这通过了您的测试:

(定义结构单元(xy))
(定义(邻接单元)
(名单
(生成单元(单元-x单元)(+(单元-y单元)1))
(制造电池(电池-x电池)((电池-y电池)1))
(生成单元(-(单元-x单元)1)(单元-y单元))
(生成单元格(+(单元格-x单元格)1)(单元格-y单元格)))
(定义(随机相邻单元)
(let((邻域(邻接单元)))
(列出ref邻域(随机(长度邻域((()()))))
(选中expect(成员)(随机相邻(使单元格123 104))
(列表(将单元格设为123和105)
(将第123和103单元格设为空)
(使单元格122变为104)
(将单元格设为124(104)))
#(对)

没有必要提出一个问题,因为这样做是行不通的。有没有办法我可以通过电子邮件向您询问更多问题?没有必要这样做。我正在定期浏览这个网站,当我不在的时候,其他人也可以帮助你。除非我们就适当的小时费率达成一致;-)当然只是开玩笑。