2015-11-20 98 views
0

我不知道如何让这段代码起作用,这么晚在这里,我认为我的大脑已经停止运作,任何人都在帮我一把吗?球拍 - 从列表中挑选一个随机元素

至今代码:

(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))) 

这里是我得到难倒,我怎么解决这个问题? 注意:下面的代码不起作用。

; 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))) 

这是它的行为应该是这样的:

(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) 
+0

有没有必要磕碰问题,不工作在SO上。 – uselpa

回答

1

这通过你的测试:

(define-struct Cell (x y)) 

(define (adjacents cell) 
    (list 
    (make-Cell (Cell-x cell) (+ (Cell-y cell) 1)) 
    (make-Cell (Cell-x cell) (- (Cell-y cell) 1)) 
    (make-Cell (- (Cell-x cell) 1) (Cell-y cell)) 
    (make-Cell (+ (Cell-x cell) 1) (Cell-y cell)))) 

(define (random-adjacent cell) 
    (let ((neighbors (adjacents cell))) 
    (list-ref neighbors (random (length neighbors))))) 

(check-expect (member? (random-adjacent (make-Cell 123 104)) 
         (list (make-Cell 123 105) 
          (make-Cell 123 103) 
          (make-Cell 122 104) 
          (make-Cell 124 104))) 
       #true) 
+0

非常感谢你! – Theo

+0

有什么方法可以通过电子邮件发送进一步问题吗? – Theo

+1

这不是必需的。我正在定期查看本网站,而其他人在我无法使用时可以帮助您更好或更好。除非我们同意适当的小时费率;-)当然只是在开玩笑。 – uselpa

相关问题