我的任务是解决一个迷宫,该迷宫使用 Racket 表示为隐式图。我尝试使用深度优先搜索来执行此操作,并且递归一直工作到必须返回并遵循不同路径的位置,在此处我收到错误:
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))])
)
)
)
))
知道为什么会发生这种情况吗?
最佳答案
这是由于缩进不良而发生的...
删除包围 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))])))))
关于scheme - Racket 上的回溯问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40979541/