2016-12-05 3 views
2

Ich wurde als eine Aufgabe zur Lösung eines Irrgartens gegeben, die als implizite Grafik mit Racket dargestellt wird. Ich versuche, so zu tun, erste Suche mit Tiefe und die Rekursion arbeitet ganzen Weg bis dorthin, wo es einem anderen Weg zurück hat und zu folgen, wo ich den Fehler:Probleme mit Backtracking auf Racket

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

Hier ist mein Code:

#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))]) 
     ) 
     ) 
    ) 
)) 

Eine Idee, warum das passiert?

Antwort

2

Dies wird aufgrund der schlechten Einbuchtung happenning ...

den Satz von Klammern entfernen, wenn Verpackung:

(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))])))))