João Areias
João Areias

Reputation: 1428

Problems with backtracking on Racket

I was given as an assignment to solve a maze that is represented as an implicit graph using Racket. I am trying to do so using depth first search and the recursion is working all the way up to where it has to return and follow a different path, where I get the error:

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

Here is my 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))])
         )
       )
    )
))

Any idea on why this is happening?

Upvotes: 2

Views: 316

Answers (1)

soegaard
soegaard

Reputation: 31147

This is happenning due to poor indentation...

Remove the set of parentheses wrapping the if:

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

Upvotes: 2

Related Questions