这段lisp代码的球拍翻译是什么?

时间:2016-03-23 19:11:47

标签: scheme lisp racket sudoku

;; 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))
        )
     )

这是数独求解器的解决方案的一部分。

1 个答案:

答案 0 :(得分:1)

尝试以下方法,它是问题中Lisp代码的功能等同物,但用Racket编写:

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

如果文件不存在,上面的代码会处理异常,并确保在读取文件后关闭该端口。