2016-03-19 7 views
0

以下の関数の隣にある関数からエラーを定義しています。定義の内側から "let"を使用する必要がありますか?define:変数が期待されていますが、部品が見つかりました

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

私は関数ネイバーの結果を関数heighbours-in-gridに入れようとしています – bruno

+0

これは問題ありません。つまり、あなたが*グリッド内のネイバー関数を呼び出すときには、ネイバー関数を使用することになります。たとえば、あなたのcheck-expectには '(近隣の人(ランダムセルa-maze))'のフォームが含まれている可能性があります。 –

+0

DrRacketの 'stepper'ツールはあなたにとって有益でしょう。 –

答えて

0

ここでの問題は、neighbours-in-grid機能の最初の行と関係があります。

具体的には、関数はこの形状を使用して定義されます。このパターンに適合しない

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

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

あなたの関数の最初の行は次のようになります。議論の名前は何ですか?この行に続くコードに基づいて、それはneighboursという単一の引数が必要なように私に見えます。もしそうなら、このコードではなく、次のようになります。

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

を私はここで混乱の一部は別途、あなたが機能という名前の隣人を持っているという事実から生じるかもしれないと思います。

関連する問題