Scheme 这段lisp代码的快速翻译是什么?
这是数独解算器解决方案的一部分。尝试以下方法,这是问题中Lisp代码的函数等价物,但是用Racket编写的:Scheme 这段lisp代码的快速翻译是什么?,scheme,lisp,racket,sudoku,Scheme,Lisp,Racket,Sudoku,这是数独解算器解决方案的一部分。尝试以下方法,这是问题中Lisp代码的函数等价物,但是用Racket编写的: ;; loads a board from the given file ;; it expects the board to be in the format of a single S-expression: ;; a list of nine lists, each containing 9 numbers (defun get-board-from-file fi
;; loads a board from the given file
;; it expects the board to be in the format of a single S-expression:
;; a list of nine lists, each containing 9 numbers
(defun get-board-from-file file
(let ((in (open file :if-does-not-exist nil)))
(when in (return-from get-board-from-file (read in)))
(when (not in) (format t "~%Unable to open file ~A" file))
)
)
如果文件不存在,上述代码将处理异常,并确保在读取文件后关闭端口。您在Racket中尝试了什么?你的翻译中有什么具体的东西不起作用吗?我一点也不知道。我试图通过Lisp代码的小翻译来学习它,我以前使用过。欢迎使用StackOverflow!我们鼓励您首先尝试一下,向我们展示您的想法,然后我们可以帮助您克服遇到的具体障碍。一般来说,人们不会简单地翻译代码。带有
(当在…
和(当(不在…)
的部分看起来不像惯用的Lisp,如果或cond
会更好地表达意图。这是打字错误吗?defun
的参数列表应该是(file)
,而不是file
。此外,我同意Óscar López关于自定义错误处理的观点:这看起来真的没有必要。你不关闭打开的流,这很糟糕。我会写(打开文件(在文件中)(let((*read eval*nil))(read in))
(define (get-board-from-file file)
(with-handlers ([exn:fail:filesystem?
(lambda (exn) (printf "~%Unable to open file ~A" file))])
(call-with-input-file file
(lambda (in) (read in)))))