2016-12-05 1 views
2

J'ai reçu une affectation pour résoudre un labyrinthe représenté sous forme de graphe implicite à l'aide de Raquette. Je suis en train de le faire en utilisant la profondeur première recherche et la récursion travaille tout le chemin jusqu'à l'endroit où il doit revenir et suivre un chemin différent, où je reçois l'erreur:Problèmes de retour arrière sur la raquette

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

Voici mon 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))]) 
     ) 
     ) 
    ) 
)) 

Une idée sur pourquoi cela se passe-t-il?

Répondre

2

Ceci est happenning en raison d'une mauvaise indentation ...

Retirez l'ensemble des parenthèses d'emballage du si:

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