2016-12-05 75 views
2

我被賦予作爲一個任務來解決迷宮,使用球拍表示爲一個隱式圖。我想這樣做,使用深度優先搜索和遞歸正在一路攀升到它必須返回,並按照不同的路徑,在這裏我得到的錯誤:球拍回溯問題

application: not a procedure; 
expected a procedure that can be applied to arguments 
    given: #<void> 
    arguments...: 
    #<void> 

這裏是我的代碼:

#lang racket 

;; Maze as a matrix as input for the function 
;; # - Represents blocked path 
;; " " - Represents a blank space 
;; . - Represents a path 

(define maze '(
     ("#" "#" "#" "#" "#" "#" "#" "#" "#" "#") 
     ("#" "I" "#" " " " " " " " " " " " " "#") 
     ("#" " " "#" " " "#" "#" "#" "#" " " "#") 
     ("#" " " " " " " "#" " " " " "#" " " "#") 
     ("#" " " "#" " " "#" "#" " " " " " " "#") 
     ("#" " " "#" " " " " " " "#" "#" " " "#") 
     ("#" " " "#" "#" "#" " " "#" "F" " " "#") 
     ("#" " " "#" " " "#" " " "#" "#" "#" "#") 
     ("#" " " " " " " "#" " " " " " " " " "#") 
     ("#" "#" "#" "#" "#" "#" "#" "#" "#" "#") 
    ) 
) 

;; Prints the maze 
(define print (λ(x)(
    if (not (empty? x)) 
     (begin 
     (writeln (car x)) 
     (print (cdr x)) 
     ) 
     (writeln 'Done) 
))) 


;; Get the element on the position (x,y) of the matrix 
(define get (λ(lst x y) (
    list-ref (list-ref lst y) x) 
)) 
;; Sets element on the position (x,y) of the matrix 
(define set (λ(lst x y symbl)(
    list-set lst y (list-set (list-ref lst y) x symbl) 
))) 

;; Searches path on maze 
(define dfs (λ(lab x y)(
    (if (string=? (get lab x y) "F") 
     (print lab) 
     (begin0 
     (cond [(or (string=? (get lab (+ x 1) y) " ") (string=? (get lab (+ x 1) y) "F")) (dfs (set lab x y ".") (+ x 1) y)]) 
     (cond [(or (string=? (get lab (- x 1) y) " ") (string=? (get lab (- x 1) y) "F")) (dfs (set lab x y ".") (- x 1) y)]) 
     (cond [(or (string=? (get lab x (+ y 1)) " ") (string=? (get lab x (+ y 1)) "F")) (dfs (set lab x y ".") x (+ y 1))]) 
     (cond [(or (string=? (get lab x (- y 1)) " ") (string=? (get lab x (- y 1)) "F")) (dfs (set lab x y ".") x (- y 1))]) 
     ) 
     ) 
    ) 
)) 

任何想法爲什麼會發生這種情況?

回答

2

這是由於壓痕差happenning ...

刪除括號包裹如果:

(define dfs 
    (λ (lab x y) 
    (if (string=? (get lab x y) "F") 
     (print lab) 
     (begin0 
      (cond [(or (string=? (get lab (+ x 1) y) " ") 
        (string=? (get lab (+ x 1) y) "F")) 
       (dfs (set lab x y ".") (+ x 1) y)]) 
      (cond [(or (string=? (get lab (- x 1) y) " ") 
        (string=? (get lab (- x 1) y) "F")) 
       (dfs (set lab x y ".") (- x 1) y)]) 
      (cond [(or (string=? (get lab x (+ y 1)) " ") 
        (string=? (get lab x (+ y 1)) "F")) 
       (dfs (set lab x y ".") x (+ y 1))]) 
      (cond [(or (string=? (get lab x (- y 1)) " ") 
        (string=? (get lab x (- y 1)) "F")) 
       (dfs (set lab x y ".") x (- y 1))])))))