2016-03-23 51 views
-1
;; 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)) 
     ) 
    ) 

這是一個數獨求解器解決方案的一部分。這段lisp代碼的球拍翻譯是什麼?

+1

你在Racket上試過了什麼?您的翻譯中某些具體內容是否無效? –

+0

我根本不認識球拍。我試圖通過Lisp代碼的小型翻譯來學習,我之前曾使用它。 – McFiddlyWiddly

+3

歡迎來到StackOverflow!我們鼓勵你先給它一個裂縫,告訴我們你想出了什麼,然後我們可以幫助你克服你遇到的具體障礙。通常,人們不會簡單地翻譯代碼。 –

回答

1

嘗試以下,這是在問題的Lisp代碼的功能相當,但寫在球拍:

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

上面的代碼,如果該文件不存在,處理異常,並確保在讀取文件後端口關閉。