2016-12-05 63 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))])))))