2015-11-20 119 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

相關問題