2016-03-19 20 views
0

Ben aşağıda komşuları-içinde-işlevinden hata tanımlamak alıyorum, içinde tanımlamak için "let" kullanmam gerekir ???define: bir değişken bekleniyor, ama bir parça bulundu

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

İşlev komşularından oluşan işlev sonucunu, yol kenarındaki – bruno

+0

Yep işlevine girmeye çalışıyorum, bu iyi. Bu demek oluyor ki * komşuları ızgara işlevini * çağırdığınızda, komşular işlevini kullanmak isteyeceksiniz. Yani, örneğin, kontrol beklediğiniz gibi (komşular (komşular (rastgele hücreli a-labirent)) '' –

+0

DrRacket'in 'step' aracını burada bulabilirsiniz. –

cevap

0

Buradaki sorun sizin neighbours-in-grid fonksiyonun ilk satırı ile ilgisi var.

Özellikle, bir fonksiyon bu şekli kullanılarak tanımlanır:

(define (<name-of-function> <name-of-argument> ...) 
    <body-of-function>) 

Kişisel işlevin ilk satırı şu şekildedir:

(define (neighbours-in-grid (neighbours (random-cell a-maze))) 
    (cond ...)) 

Bu desen uymuyor. Argümanların isimleri nelerdir? Bu satırı takip eden koda dayanarak, bana neighbours isimli tek bir argüman istediğiniz gibi görünüyor. Eğer öyleyse, bu kod yerine okumalısınız:

(define (neighbours-in-grid neighbours) 
    (cond ...)) 

Burada karışıklık parçası ayrı ayrı, bir işlev adında komşularımız var aslında kaynaklanıyor olabileceğini düşünüyorum.

İlgili konular