scheme - Racket 上的回溯问题

标签 scheme lisp racket depth-first-search

我的任务是解决一个迷宫,该迷宫使用 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/

相关文章:

scheme - Racket 方案 - 从流中删除重复的连续字符

types - Scheme 中类型的确切术语

scheme - 为什么 Lisp 文件不是语句列表?

lisp - 如何测试符号中的第一个字符是否是 lisp 中的字母?

scheme - 函数 count 应接受两个参数 : an atom and a simple list. 该函数应返回在列表中找到原子的次数

lambda - 在 Scheme 中消除 lambda?

dynamic - 如何在Scheme中获取值的类型?

scheme - 无限流的累加器

data-structures - 流实现

scheme - 简化递归函数语法的宏