gpt4 book ai didi

scheme - Racket 上的回溯问题

转载 作者:太空宇宙 更新时间:2023-11-03 18:48:02 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com